chillpill

有哪位大手子用过 FormData+connect-busboy 上传文件么

  •  
  •   chillpill · Oct 26, 2017 · 1833 views
    This topic created in 3124 days ago, the information mentioned may be changed or developed.

    如题,lz 在学习用 FormData 上传文件没有响应。

    前端 js 文件: var fd= new FormData($('#musicup')[0]);//#musicup 是一个 form 表单

    $.ajax({ url: '/upload' , method: 'POST', data: fd, async: false, cache: false, contentType: false, processData: false, success: function (msg) { alert("success with "+msg); }, error: function (msg) { alert("fail with "+msg); } });

    浏览器用的 firefox, 提交的时候最初没有任何消息头产生,但过了很长一段时间(大概是某个地方本身设了超时时间的原因)又会有请求头产生(没有响应头)。content-type 是 multipart/form-data;boundary...这些。 然后又改用 chrome,立即产生的头,content-type 还是 multipart/form-data...,formdata 的内容在 request payload 里面看得到,都是正确的。

    后端用的 node keystone,其实是封装的 express+mongodb 的框架,之前在网上搜了一下,用了一个 connect 中间件 connect-busboy,

    后端 js: req.pipe(req.busboy); req.busboy.on('file',function(fieldname,file,filename){ console.log("Uploading: " + filename); //一些处理 }); req.busboy.on('data',function(data){ console.log(data); }); req.busboy.on('field',function(fieldname, val, fieldnametruncated, valtruncated){ console.log(fieldname+", "+ inspect(val)); }); req.busboy.on('finish',function(){ console.log("finished uploading"); });

    结果只有 finish 事件触发了,file,data,field 都没有触发

    哪位大神带带路。。。T T 纠结了好久了。。

    chillpill
        1
    chillpill  
    OP
       Oct 26, 2017
    啊啊啊啊啊都快疯了。。
    补充进度。。。
    经笨办法调试,formdata 提交的数据用 req.body 打印(没有用中间件处理的)
    始终没有文件数据,input text 之类的都有
    T T
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2833 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:05 · PVG 20:05 · LAX 05:05 · JFK 08:05
    ♥ Do have faith in what you're doing.