什么是上传文件的ASP.NET MVC 2的最佳方式? [英] What is the best way to upload files with ASP.NET MVC 2?

查看:174
本文介绍了什么是上传文件的ASP.NET MVC 2的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是可变的上传大小的文件的最佳方法(要么非常大或非常小的一个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)处理的控制器动作上传 Request.Files HttpPostedFileBase ,里面好像有的花费很长的时间,因为ASP.NET将这些文件到活动内存的缺点。

(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.

(2)拦截文件上传早与这在某种程度上规避性能问题的HTTP模块。 (我是如何工作有点浑浊,但我已经使用这个帖子的 http://darrenjohnstone.net/2008/07/15/aspnet-file-upload-module-version-2-beta-1/ 作为参考) 。 我模糊有关的部分是在什么时候ASP.NET加载提交文件到活动内存,以及如何在一个模块中截获这实际上改变了这种行为。

(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.

由于第二个选项是速度更快,这似乎是更好的选择。但它似乎是一个应用程序提交上传表单将可能有与需要在数据库要永久保存的文件相关的一些数据。我不想做持续性的呼叫我的HttpHandler或HTTP模块,(因为那时我将在不同的地方出现两个非常相似的功能:控制器和HTTP处理程序)。

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).

我想一个解决将存储在HttpContext.Items目标文件位置,但是最好的方法?

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

这个最后一个问题是,我想渲染的Htt presponse文件已完成,然后再上传。所以,如果有一个大的文件,我会向用户发送视图与上传状态值,使AJAX调用保持更新状态。 如何渲染的结果,同时保持上载过程中去?我是否需要作出AsyncHandler或AsyncController?我是否需要手动抓住另一个线程?

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?

非常感谢球员。我知道这是一个很大的问题,而且很可能反映了一个普遍缺乏了解的东西。了解有关一般有趣的缺乏是谁拥有它们的人也往往缺乏他们缺乏理解什么......所以,如果有人能指出我关于这一点正确的方向为好,我会$ AP的理解p $ pciate它。

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.

推荐答案

如果我没记错的话从ASP.NET 2.0的大文件被刷新到磁盘上,所以即使使用HttpPostedFileBase不应该有任何内存/性能问题。我在这里不是很确定asynccontrollers是一种解决方案,asynccontrollers是长时间运行的服务器进程。举一个例子断AsyncControllers看到的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

这篇关于什么是上传文件的ASP.NET MVC 2的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆