开发者社区> 问答> 正文

在html5中jquery如何用ajax上传文件

现在最新的firefox和chrome都已经支持用FormData来自定义multipart/form-data的具体字段,这样用ajax上传文件就成了可能,但我在使用最新版jquery的ajax方法上传文件时,却遇到了问题,jquery默认设置的contentType不对,导致后端无法正确识别文件上传请求

这是使用原生XHR来上传文件时的header截图,我们可以清楚的看到Content-Type已经做了特殊设定,这时后端可以正确的接收到文件
screenshot

展开
收起
a123456678 2016-07-29 10:33:05 3369 0
1 条回答
写回答
取消 提交回答
  • var fd = new FormData(document.getElementById("fileinfo"));
    fd.append("CustomField", "This is some extra data");
    $.ajax({
      url: "stash.php",
      type: "POST",
      data: fd,
      processData: false,  // tell jQuery not to process the data
      contentType: false   // tell jQuery not to set contentType
    });
    2019-07-17 20:01:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《零基础HTML入门教程》 立即下载
天猫 HTML5 互动技术实践 立即下载
天猫HTML5互动技术实践 立即下载