如何使用MVC 4 / plupload上传大文件? [英] How to upload large files using MVC 4 / plupload ?
本文介绍了如何使用MVC 4 / plupload上传大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有它的工作..但我发现当我被上传变得更大(约4000K)控制器就不叫文件。
于是我在分块其中固定资产这一问题增加..但是现在,当我打开该文件,其完整的乱码...
那么什么是上传大文件,plupload / MVC 4的正确方法?
下面是我目前的code
$(文件)。就绪(函数(){ VAR上传=新plupload.Uploader({
运行时间:HTML5,
browse_button:pickfiles',
容器:容器,
// MAX_FILE_SIZE:20000mb',
网址:'@ Url.Action(上传,家),
CHUNK_SIZE:4MB,
//过滤器:
// {标题:Excel文件,扩展名:XLS,XLSX},
// {标题:文本文件,扩展名:TXT}
//]
multiple_queues:真实,
多部分:真实,
multipart_params:{TASKID:''}
});
和所述控制器
[HttpPost]
公众的ActionResult上传(INT?大块,字符串名称,字符串TASKID)
{
字符串文件路径=;
变种文件上传= Request.Files [0];
VAR uploadPath =使用Server.Mappath(〜/ App_Data文件/上传);
大块大块=? 0;
字符串uploadedFilePath = Path.Combine(uploadPath,名);
变种文件名= Path.GetFileName(uploadedFilePath); 尝试
{
使用(VAR FS =新的FileStream(文件路径,块== 0 FileMode.Create:FileMode.Append))
{
VAR缓冲=新的字节[fileUpload.InputStream.Length]
fileUpload.InputStream.Read(缓冲液,0,buffer.Length);
fs.Write(缓冲液,0,buffer.Length);
} //登录到DB以备将来处理
InstanceExpert.AddProcessStart(文件路径,Int32.Parse(TASKID));
}
解决方案
在web.config中你需要这些(2GB各地):
<&的System.Web GT;
<编译调试=真targetFramework =4.5/>
<的httpRuntime targetFramework =4.5的maxRequestLength =2147483647executionTimeout =1600requestLengthDiskThreshold =2147483647/>
<安全>
<&的requestFiltering GT;
< requestLimits maxAllowedContentLength =2147483647/>
< /&的requestFiltering GT;
< /安全>
...
< /system.web>
I had it working.. but I noticed once the files I was uploading get bigger (around 4000k) the controller would not be called..
So I added in chunking which fixed that problem.. but now when I open the file its full of garbage characters...
So what is the correct way to upload large files with plupload/MVC 4 ?
Here is my current code
$(document).ready(function () {
var uploader = new plupload.Uploader({
runtimes: 'html5',
browse_button: 'pickfiles',
container: 'container',
// max_file_size: '20000mb',
url: '@Url.Action("Upload", "Home")',
chunk_size: '4mb',
//filters: [
// { title: "Excel files", extensions: "xls,xlsx" },
// { title: "Text files", extensions: "txt" }
//],
multiple_queues: true,
multipart: true,
multipart_params: { taskId: '' }
});
and the controller
[HttpPost]
public ActionResult Upload(int? chunk, string name, string taskId)
{
string filePath = "";
var fileUpload = Request.Files[0];
var uploadPath = Server.MapPath("~/App_Data/Uploads");
chunk = chunk ?? 0;
string uploadedFilePath = Path.Combine(uploadPath, name);
var fileName = Path.GetFileName(uploadedFilePath);
try
{
using (var fs = new FileStream(filePath, chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
//Log to DB for future processing
InstanceExpert.AddProcessStart(filePath, Int32.Parse(taskId));
}
解决方案
In web.config you need these (2GB all around):
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647" />
</requestFiltering>
</security>
...
</system.web>
这篇关于如何使用MVC 4 / plupload上传大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文