Upload/Download large files with ASP.NET Core 2.2

Asked

Viewed 542 times

0

I need to upload and downlods files above 2GB using ASP.NET Core 2.2, but I don’t know the best way to do this. For the download, to using the following method:

[HttpGet("id")]
public IActionResult Download(Guid id)
{
    // Recupero o caminho físico do arquivo com base no id aqui
    return PhysicalFile(path, MimeTypes.GetMimeType(path), Path.GetFileName(path));
}

Apparently it works normal, but I have doubts about performance or parallel requests. There is a better approach to doing this?

Already in the upload I thought of separating the file into pieces on the client side and sending these pieces in various requests. On the server side I would receive each piece, save at the end of the file and send a reply so that a new piece is sent. But... Is this really a good way to do it? I think I might have competition issues opening/closing the file at the written time.

What is the appropriate way to download/upload?

1 answer

1


You can use a form with enctype Multipart/form-data. It supports large files, sending files is done by parts only which is managed by the browser.

I made a project available on Github with a Net Core project and the file upload progress bar to the server.

I left the simplified version of the code here.

In the case of Net Core you need to indicate the maximum size that your server accepts.

Program.Cs

public class Program
    {
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseKestrel(options =>
                {
                    options.Limits.MaxRequestBodySize = 209715200;
                });
    }

Startup.Cs

    public void ConfigureServices(IServiceCollection services)
    {

         services.Configure<FormOptions>(x =>
        {
          x.MultipartBodyLengthLimit = 1209715200;
        });
    }

Front

    <form method="post" enctype="multipart/form-data" 
action="file" onsubmit="AJAXSubmit(this);return false;" >
            <input type="file" name="file"/>
            <input type="submit" value="Upload" />            
    </form>


    async function AJAXSubmit (form) {

    var formData = new FormData($(form)[0]);

    $.ajax({
        url: "file",
        type: 'POST',
        data: formData,
        async: true,
        success: function (data) {
            alert('Sent');
        },
        cache: false,
        contentType: false,
        processData: false
    });
   }

Backend

[HttpPost]
    [RequestFormLimits(MultipartBodyLengthLimit = 1209715200)]
    [RequestSizeLimit(1209715200)]
    public async Task<ActionResult> Post(IFormFile file)
    {
        FileStream filestream = new FileStream("/tmp/" + file.FileName, FileMode.Create, FileAccess.Write);

        using (var memoryStream = new MemoryStream())
        {
            await file.CopyToAsync(memoryStream);

            memoryStream.WriteTo(filestream);
        }

        return Ok(new { count = 1, file.FileName, file.Length});
    }

inserir a descrição da imagem aqui

  • 1

    I ended up choosing to use the Azure storage service, but thanks for sharing your reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.