asp.net 如何修改UploadFiles任务以接收所有文件类型?

xwbd5t1u  于 2022-11-19  发布在  .NET
关注(0)|答案(2)|浏览(104)

这是我的任务

private async Task UploadFiles(InputFileChangeEventArgs inputFileChangeEventArgs)
    {
        _CarregaFoto = true;

        var fileFormat = "image/png";

        var MAXALLOWEDSIZE = 60000000;

        var imageFile = await inputFileChangeEventArgs.File.RequestImageFileAsync(fileFormat, 6000, 6000);

        var buffer = new byte[imageFile.Size];

        await imageFile.OpenReadStream(MAXALLOWEDSIZE).ReadAsync(buffer);

        AnexoDenunciaModel _novaFoto = new AnexoDenunciaModel();

        _novaFoto.imagem = buffer;
        _novaFoto.id_ocorre = id;
        _novaFoto.nome = imageFile.Name;
        _novaFoto.Denunciante = true;

        await _db.InsertAnexoDenuncia(_novaFoto);

        _CarregaFoto = false;

        await LeTabelas2();

    }

我试过将var fileFormat更改为pdf或discart这部分,但没有成功,该任务只接受png,jpg等文件。我怎么能接受其他类型如pdf,txt文件?

flvlnr44

flvlnr441#

请看这里:https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-6.0&pivots=server

private int maxAllowedFiles = 3;
    private async Task LoadFiles(InputFileChangeEventArgs e)
    {
        foreach (var file in e.GetMultipleFiles(maxAllowedFiles))
        {
               DO YOUR STUFF
        }
    }
sc4hvdpw

sc4hvdpw2#

Blazor并没有限制你上传的文件类型。这需要你自己来完成。为了告诉<InputFile />组件应该上传哪些文件,你需要自己指定。
例如:

<InputFile OnChange="OnInputFileChange" class="form-control" accept="application/pdf" />

相关问题