weixin_33743248 2014-09-12 09:23 采纳率: 0%
浏览 133

文件上传失败

I am trying to upload file using Ajax to a Nodejs Server.

Ajax Code:

var url = 'http://<ip:port>/upload/';
var formValues = $("#files").get(0).files;
$.ajax({
    url: url,
    type: 'POST',
    data: formValues,
    processData: false,
    cache: false,
    beforeSend: function( xhr ) {
        xhr.setRequestHeader('content-type', 'multipart/form-data');
    },
    success: function (data) {
        console.log("Store details: %j", data);
        Backbone.history.navigate('store');
        window.location.reload();
    }
});

I am using busboy in the Node Server and it is giving me this error when i trying to parse the request headers to initialize the busboy object.

Error: Multipart: Boundary not found
at new Multipart (/home/ubuntu/MoojicDashboard/node_modules/busboy/lib/types/multipart.js:58:11)
at Multipart (/home/ubuntu/MoojicDashboard/node_modules/busboy/lib/types/multipart.js:26:12)
at Busboy.parseHeaders (/home/ubuntu/MoojicDashboard/node_modules/busboy/lib/main.js:62:22)
at new Busboy (/home/ubuntu/MoojicDashboard/node_modules/busboy/lib/main.js:21:10)

But if i don't set the content-type to multipart/form-data the request is discarded by the busboy.

I even tried using multer package but it also give me the same error.( Later i found out it was build on busboy.)

I even tried setting contentType to false.

$.ajax({
            url: url,
            type: 'POST',
            data: formValues,
            processData: false,
            cache: false,
            contentType: false,
            success: function (data) {
                console.log("Store details: %j", data);
                Backbone.history.navigate('store');
                window.location.reload();
            }
        }); 

to force JQuery not to set default content-type but it didn't work too.

Setting contentType: 'multipart/form-data' also not worked and give me the same 'Multipart: Boundary not found' error. So anybody can help me out to get out of this error.

  • 写回答

2条回答 默认 最新

  • larry*wei 2014-09-15 02:00
    关注

    You should use the FormData API instead of trying to do it yourself. Then when you pass your FormData instance to $.ajax(), it will automatically set the right headers for you. Here is an example.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名