Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to upload files with ASP.NET MVC 2?

What is the best method for uploading files of variable size (either very large or very small to an ASP.NET MVC 2 application file system)?

This is what I understand so far:

It seems like there are two ways that people handle this. (Let's assume the files may be very large or very small)

(1) Handle the upload in a controller action via Request.Files or HttpPostedFileBase, which seems to have a drawback of taking a long time because ASP.NET loads the files into active memory.

or

(2) intercept the file upload early on with an HttpModule which somehow circumvents the performance issue. (I'm a little cloudy on how this works, but I've been using this post http://darrenjohnstone.net/2008/07/15/aspnet-file-upload-module-version-2-beta-1/ as a reference). The part I'm fuzzy about is at what point ASP.NET loads the submitted files to active memory, and how intercepting this in a module actually changes this behavior.

Since the second option is faster, it seems like the better option. But it seems like an application submitting an upload form will probably have some data associated with the file that needs to be persisted in a database. I don't want to make persistence calls in my HttpHandler or HttpModule, (because then I will have two very similar functionalities occurring in different places : the controller and the http handler).

I guess one work around would be to store the target file location in HttpContext.Items, but is this the best way?

One last concern about this is that I want to render an HttpResponse before the file is finished uploading. So, if there is a big file, I will send the user a view with the value of the upload status, and make AJAX calls to keep the status updated. How do I render a result, while keeping the upload process going? Do I need to make an AsyncHandler or AsyncController? Do I need to manually grab another thread?

Thanks a lot guys. I know this is a lot of questions, and probably reflects a general lack of understanding about something. The funny thing about general lacks of understanding is that people who have them also tend to lack the understanding of what understanding they are lacking...so, if anyone can point me in the right direction on that note as well, I would appreciate it.

like image 866
smartcaveman Avatar asked Jan 25 '11 04:01

smartcaveman


People also ask

How can upload file in ASP.NET MVC?

Uploading a file in Asp.Net MVC application is very easy. The posted file is automatically available as a HttpPostedFileBase parameters in the action of the controller. For uploading a file on the server you required to have a file input control within html form having encoding type set to multipart/form-data.

How can upload video in ASP.NET MVC?

Right-click on project Add select New Folder, name it VideoFileUpload to upload all the audio files in that folder. Right-click on Models folder, select Add, then select Class. A window will appear. Choose Class, give it the name VideoFiles, then click on Add.

How do I upload files to Iformfile?

Upload Single FileTo add view, right click on action method and click on add view. Then select View from left side filter and select Razor View – Empty. Then click on Add button. Create design for your view as per your requirements.


1 Answers

If I remember correctly from ASP.NET 2.0 large files are being flushed to disk, so even using HttpPostedFileBase there should not be any memory/performance problems. I am not sure asynccontrollers is an solutions here, asynccontrollers is for long running server processes. For an example off AsyncControllers see http://www.aaronstannard.com/post/2011/01/06/asynchonrous-controllers-ASPNET-mvc.aspx

like image 86
Wim Avatar answered Oct 11 '22 17:10

Wim