Home > Back-end >  How can i modify my UploadFiles Task to receive all of file types?
How can i modify my UploadFiles Task to receive all of file types?

Time:11-04

Here is my Task

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();

    }

I've tried to change var fileFormat to pdf or discart this part but with no success, the task only accept png, jpg, etc. files. How can i accept other types like pdf, txt files?

CodePudding user response:

Take a look here: 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
        }
    }

CodePudding user response:

Blazor does not limit you on the possible filetypes to upload. This has to been done by yourself. In order to tell the <InputFile /> component which files should be uploaded, you'll need to specify it for yourself.

For example:

<InputFile OnChange="OnInputFileChange"  accept="application/pdf" />
  • Related