有没有办法使用HttpPostedFile类获取上传进度? [英] Is there a way to get upload progress using the HttpPostedFile class?

查看:154
本文介绍了有没有办法使用HttpPostedFile类获取上传进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 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定期将状态返回到网页,以便显示进度条和完成百分比?

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总是在你使用HttpRequest.InputStream时加载整个请求内容。

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

如果你想提供这样的反馈,你需要做在客户端使用类似flash的东西,或者编写自己的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.

这篇关于有没有办法使用HttpPostedFile类获取上传进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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