如何使用 ASP.NET MVC4 Web Api 上传大文件
也有进步?
我看到了这篇文章,我知道如何处理上传的文件,但我如何才能获得进度数据? How To Accept a File POST
请不要向我发送上传产品的链接。 我想了解如何以 MVC4 Web Api 方式处理此问题... 这是在 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)
我怎样才能知道字节是如何加载的?
最佳答案
有两个地方默认上传的文件大小有限制。一个在请求级别,第二个,如果您托管在 IIS 上,则在 Web 服务器级别。我添加了几个配置,如 this blog 中所述,我能够毫无问题地上传一个 36mb 的文件。我已经发布了下面的代码片段。
基本上
1.
<system.web>
<httpRuntime maxRequestLength="2097152"/>
</system.web>
2.
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security><system.webServer>
如果您愿意,很容易找到加载到服务器中的文件的大小。在你的代码中
在读取流中的文件数据时,对于文件数据中的每个项目,您可以读取本地文件名,如下所示。
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
希望这对您有所帮助。我想知道为什么这会被 Markdown ?
关于c# - 如何使用带进度条的 ASP.NET MVC4 Web Api 上传大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506648/