如何上传与进度ASP.NET MVC4网页API一个大文件 [英] how to upload a large file with ASP.NET MVC4 Web Api with progressbar

查看:119
本文介绍了如何上传与进度ASP.NET MVC4网页API一个大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何上传与ASP.NET MVC4网页API结果大文件
并且也得到了进步?

how can i upload a large file with ASP.NET MVC4 Web Api
and also get a progress?

我看到这个帖子,我知道如何处理上传的文件,但我怎么能得到进度数据?
<一href=\"http://stackoverflow.com/questions/10320232/how-to-accept-a-file-post-asp-net-mvc-4-webapi\">How接受文件POST - ASP.Net MVC 4的WebAPI

i saw this post and i understand how to handle the uploaded file but how i can get the progress data? How To Accept a File POST - ASP.Net MVC 4 WebAPI

请不要把我的链接上传产品。
我想了解如何在MVC4网页API的方式处理这个问题?
这里是在MVC4的WebAPI

please don't send me links to upload products. i want to understand how handle this in the MVC4 Web Api way... here is an example code of handling a file upload in MVC4 WebApi

    public async Task<HttpResponseMessage> Post()
    {
        if (Request.Content.IsMimeMultipartContent())
        {
            var path = HttpContext.Current.Server.MapPath("~/App_Data");

            var provider = new MultipartFormDataStreamProvider(path);

            await Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
            });

            return Request.CreateResponse(HttpStatusCode.OK); 
        }
        else
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
        }
    }

现在,当

   await Request.Content.ReadAsMultipartAsync(provider)

如何能得到怎样装字节?

how can i get how bytes loaded?

推荐答案

有一个限制对文件的大小被默认在两个地方上载。一个在请求级别,第二,如果你托管在IIS上,然后在Web服务器级别。我加几的configs在这个博客提到的,我能够上传36MB文件,没有任何问题。我已经发布下面的代码片段。

There is a limitation to the size of files to be uploaded by default at two places. One at the request level, and second , if you hosting on IIS, then on web server level. I added couple of configs as mentioned in this blog, and i was able to upload a 36mb file without any issues. I have posted the snippet below.

基本上

1

  <system.web> 
    <httpRuntime maxRequestLength="2097152"/>
  </system.web>

2

<system.webServer> 
  <security> 
      <requestFiltering> 
         <requestLimits maxAllowedContentLength="2147483648" /> 
      </requestFiltering> 
  </security><system.webServer> 

它很容易找到加载到服务器,如果您希望文件的大小。在您的code

Its easy to find the size of the file loaded into the server if you wish. In your code

而读通过流在FILEDATA,在你的文件数据的每个项目,可以读本地文件名,如下图所示。

while reading through the filedata in the stream, for each item in you file data, you can read the local file name as shown below.

 string savedFile = fileData.LocalFileName;
 // use the file info class to derive properties of the uploaded file
 FileInfo file = new FileInfo(savedFile);
//this will give the size of the uploaded file 
long size = file.length/1024

希望这有助于。我不知道为什么这个被打上了吗?

Hope this helps. I wonder why this was marked down?

这篇关于如何上传与进度ASP.NET MVC4网页API一个大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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