使用 ASP.NET MVC 2 上传文件的最佳方式是什么?
上传可变大小的文件(对于 ASP.NET MVC 2 应用程序文件系统来说非常大或非常小)的最佳方法是什么?
到目前为止我的理解是:
人们似乎有两种处理这个问题的方法。 (假设文件可能非常大或非常小)
(1)通过 Request.Files
或 HttpPostedFileBase
在控制器操作中处理上传,这似乎有一个缺点由于 ASP.NET 将文件加载到活动内存中,因此需要很长时间。
或者
(2) 使用 HttpModule 尽早拦截文件上传,这以某种方式规避了性能问题。 (我对它的工作原理有点不清楚,但我一直在使用这篇文章 http://darrenjohnstone.net/2008/07/15/aspnet-file-upload-module-version-2-beta-1/ 作为参考) 。 我模糊的部分是 ASP.NET 在什么时候将提交的文件加载到活动内存,以及在模块中拦截它实际上如何改变这种行为。
由于第二个选项更快,所以看起来喜欢更好的选择。但提交上传表单的应用程序似乎可能会有一些与需要保存在数据库中的文件关联的数据。我不想在我的 HttpHandler 或 HttpModule 中进行持久性调用(因为这样我将在不同的地方出现两个非常相似的功能:控制器和 http 处理程序)。
我想一种解决方法是将目标文件位置存储在 HttpContext.Items 中,但是这是最好的方法吗?
最后一个问题是我想在文件完成之前渲染 HttpResponse正在上传。因此,如果有一个大文件,我将向用户发送一个包含上传状态值的视图,并进行 AJAX 调用以保持状态更新。 如何在保持上传过程继续的同时呈现结果?我需要创建 AsyncHandler 或 AsyncController 吗?我需要手动抓取另一个线程吗?
非常感谢大家。我知道这是很多问题,并且可能反映出人们普遍缺乏对某些事物的理解。关于普遍缺乏理解的有趣之处在于,拥有这些理解的人也往往不了解他们所缺乏的理解……所以,如果有人也能在这方面指出我正确的方向,我将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没记错的话,从 ASP.NET 2.0 开始,大文件就会被刷新到磁盘,所以即使使用 HttpPostedFileBase 也不应该出现任何内存/性能问题。我不确定 asynccontrollers 是这里的解决方案,asynccontrollers 适用于长时间运行的服务器进程。有关 AsyncController 的示例,请参阅 http://www .aaronstannard.com/post/2011/01/06/asynchonrous-controllers-ASPNET-mvc.aspx
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
我使用这个 javascript 工具
这是控制器(我仔细检查,因为 IE 有一个奇怪的行为):
我把这个 HTML 放在我的视图中:
这是 javascript:
I use this javascript tool
This is the controller (I double check cause IE has a strange behavior) :
I put this HTML in my View:
and this is the javascript: