Search code examples
c#.net-corefilestream

Upload large file async without disposing it


I'm building a .NET Core 3.1 WEB API that will receive a large file(up to 500MB), if it's valid, return a 200 response to the client and only after that, upload the uploaded file to an Azure Blob Container.

The issue I've got is that the Request/Stream is being disposed(I believe) after I return the response to the client, and it doesn't give me any errors nor is it uploaded.

I've tried a few alternatives but with no success:

  • Task.Run()
  • Locking the stream,
  • Copying the stream to a new variable
  • Synchronising the stream,
  • Hosted Service with a Background Queue

The only way the file is successfully uploaded is if either I persist the file locally or force the user to wait, but I would rather not do that if I can.

Has anyone got an idea how I can accomplish this?


EDIT:

I create a resource on the database with the file upload operation( and return a 202 with the file upload operation id) and I update it's status depending on the success of the upload to the Azure Blob Storage.

The user can check the upload status on a different API endpoint.

I would just like to know if there's a way of persisting the file stream or any object really after the response is returned.


Solution

  • If the browser provided an Expect: Continue header. Then you can fail a request based on the provided request headers, before the client has sent any file data. Or then respond with a 100 continue and read the file data.

    However, if you want to keep that file data, you will have to copy it somewhere before you can return a status code to the client. Into memory, onto a local disk, or stream it all the way to your back end blob container. That's just how HTTP works.