有没有一种方法使用HttpPostedFile类来获取上传进度?进度、上传、方法、HttpPostedFile

2023-09-11 00:55:54 作者:酒煙妓

我想使用 HttpPostedFile 类以上传一个或多个大型文件从网页中的ASP.NET MVC控制器。使用这个类,上传的文件大于256 KB的缓存到磁盘上,而不是在服务器内存中。

I want to use the HttpPostedFile Class to upload one or more large files to an ASP.NET MVC controller from a web page. Using this class, uploaded files larger than 256 KB are buffered to disk, rather than held in server memory.

我的理解是,它可以这样做:

My understanding is that it can be done like this:

if (context.Request.Files.Count > 0)
{
    string tempFile = context.Request.PhysicalApplicationPath;
    for(int i = 0; i < context.Request.Files.Count; i++)
    {
        HttpPostedFile uploadFile = context.Request.Files[i];
        if (uploadFile.ContentLength > 0)
        {
            uploadFile.SaveAs(string.Format("{0}{1}{2}",
              tempFile,"Upload\\", uploadFile.FileName));
        }
    }
}

有没有办法用一些其他的方法来设置一个回调,或定期回访状态通过AJAX或JSON网页,以便完成一个进度条和百分比可以显示?你会在code样子?

Is there a way to set a callback, or using some other method, return status periodically to the web page via AJAX or JSON so that a progress bar and percent completed can be displayed? What would the code look like?

推荐答案

都能跟得上。 Asp.net始终只要您使用的Htt prequest.InputStream加载整个请求的内容。

Nope. Asp.net always load the whole of the request content as soon as you use HttpRequest.InputStream.

如果你想提供这样的反馈,您可能需要使用类似闪光灯这样做的客户端,或写你自己的HTTP hanlder,将直接进入的HttpWorkerRequest方法来自己加载实体主体。

If you want to provide such a feedback, you either need to do it on the client-side using something like flash, or write your own http hanlder that would go straight to the HttpWorkerRequest methods to load the entity body yourself.