Dealing with large file uploads on ASP.NET Core 1.0

前端 未结 3 602
闹比i
闹比i 2020-12-04 17:52

When I\'m uploading large files to my web api in ASP.NET Core, the runtime will load the file into memory before my function for processing and storing the upload is fired.

3条回答
  •  清歌不尽
    2020-12-04 18:57

    In your Controller you can simply use Request.Form.Files to access the files:

    [HttpPost("upload")]
    public async Task UploadAsync(CancellationToken cancellationToken)
    {
        if (!Request.HasFormContentType)
            return BadRequest();
    
        var form = Request.Form;
        foreach(var formFile in form.Files)
        {
            using(var readStream = formFile.OpenReadStream())
            {
                // Do something with the uploaded file
            }
        }
    
    
        return Ok();
    }
    

提交回复
热议问题