加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何使用MVC 4上传大文件?

发布时间:2020-12-15 19:07:29 所属栏目:asp.Net 来源:网络整理
导读:我有它的工作..但我注意到,一旦我上传的文件越来越大(大约4000k)控制器不会被调用.. 所以我添加在chunking哪个修复了这个问题..但现在当我打开的文件,其完整的垃圾字符… 那么什么是正确的方式上传大文件与plupload / MVC 4? 这是我当前的代码 $(document
我有它的工作..但我注意到,一旦我上传的文件越来越大(大约4000k)控制器不会被调用..

所以我添加在chunking哪个修复了这个问题..但现在当我打开的文件,其完整的垃圾字符…

那么什么是正确的方式上传大文件与plupload / MVC 4?

这是我当前的代码

$(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: '' }
    });

和控制器

[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,buffer.Length);
                fs.Write(buffer,buffer.Length);
            }

            //Log to DB for future processing
            InstanceExpert.AddProcessStart(filePath,Int32.Parse(taskId));
        }

解决方法

在web.config你需要这些(2GB四周):
<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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读