【问题标题】:.pdf/mp4 upload from JQuery-AJAX to MVC controller method.pdf/mp4 从 JQuery-AJAX 上传到 MVC 控制器方法
【发布时间】:2015-02-10 13:13:14
【问题描述】:

我正在尝试将 .pdf/.mp4 文件从 JQuery-AJAX 发送到 mvc 控制器。这是我的代码-

    //JQuery-AJAX
    $('#fileUpload').change(function (e) {
    var file = e.target.files[0];
    fd.append("photos",  file);
    fd.append("CourseId", courseid);
    fd.append("LessonId", lessonid);      
    $.ajax({
        url: '/Admin/Course/SaveFile',
        type: 'POST',
        data: fd,
        cache: false,
        contentType: false,
        processData: false,
        success: handler,
        error: handler
     });
 });

  //server-side
  public JsonResult SaveFile(IEnumerable<HttpPostedFileBase> photos, FormCollection data)
  {
     var courseId = data["CourseId"].ToString());
     .....
     foreach (var file in photos)
     {
        file.SaveAs(somePath);
     }
     ....
  }

但是如果contentType: false,请求不会转到控制器方法。

contentType: "multipart/form-data" 时,它会命中控制器方法但data["CourseId"] 会抛出异常object reference not set ...

但我使用相同的代码很好地上传图片。

有什么帮助吗?

【问题讨论】:

标签: jquery ajax asp.net-mvc file-upload


【解决方案1】:

在 web.config 中添加这些行解决了我的问题-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 2019-02-16
    • 1970-01-01
    相关资源
    最近更新 更多