【问题标题】:Unsupported content type Image/PNG不支持的内容类型图像/PNG
【发布时间】:2014-10-01 02:07:17
【问题描述】:

我正在尝试使用 NodeJS busboy 在服务器上上传图像文件,但出现此错误:

Service Listening for request on: 8080
Error: Unsupported content type: image/png
    at Busboy.parseHeaders (D:\ImageUploadService\node_modules\busboy\lib\main.j
s:66:9)
    at new Busboy (D:\ImageUploadService\node_modules\busboy\lib\main.js:21:10)
    at D:\ImageUploadService\server.js:15:15
    at Layer.handle [as handle_request] (D:\ImageUploadService\node_modules\expr
ess\lib\router\layer.js:76:5)
    at next (D:\ImageUploadService\node_modules\express\lib\router\route.js:100:
13)
    at Route.dispatch (D:\ImageUploadService\node_modules\express\lib\router\rou
te.js:81:3)
    at Layer.handle [as handle_request] (D:\ImageUploadService\node_modules\expr
ess\lib\router\layer.js:76:5)
    at D:\ImageUploadService\node_modules\express\lib\router\index.js:234:24
    at Function.proto.process_params (D:\ImageUploadService\node_modules\express
\lib\router\index.js:312:12)
    at D:\ImageUploadService\node_modules\express\lib\router\index.js:228:12

下面是我的代码:

app.post('/uploadImage',function(req,res){
    var alias=req.query.alias;
    var imagetype=req.query.imagetype;  //can be media/profile
    var busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        var saveTo = ".\\Images\\"+alias+"\\"+imagetype+"\\"+filename;
        if (fs.existsSync(saveTo)) {
            file.pipe(fs.createWriteStream(saveTo));
        }
        else{
            fs.mkdir(".\\Images\\"+alias+"\\"+imagetype,function(err){
                 saveTo=".\\Images\\"+alias+"\\"+imagetype;
                 file.pipe(fs.createWriteStream(saveTo));
            });
        }   
    });
    busboy.on('finish', function() {
      res.writeHead(200, { 'Connection': 'close' });
      res.status(200).end();
    });
    return req.pipe(busboy);
});

我正在尝试使用POSTMAN REST CLIENT 发出请求。

这是客户的问题还是我必须在代码中进行更改。请注意,我在客户端(邮递员)上提到了Content-Type: image/png

附带问题:另外,我有办法将图像存储为拇指??

【问题讨论】:

    标签: node.js fs busboy


    【解决方案1】:

    Busboy 仅解析 application/x-www-form-urlencodedmultipart/form-data 请求。如果您要发送原始文件数据,则必须手动处理(这很简单,因为不涉及解析)。

    【讨论】:

    • 那我怎么上传图片呢?你可以推荐任何图书馆吗?或任何例子??
    • Postman 支持 HTML 表单提交,只需将其与 multipart/form-dataContent-Type 一起使用并将您的文件添加到表单中。
    • 也许您可以发布您的 Postman 设置/设置的屏幕截图?
    • 我用 fiddler 试了一下,效果很好。不知道它不能与邮递员合作吗?
    猜你喜欢
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多