如何使用Nodejs

时间:2017-05-03 08:41:52

标签: node.js parse-platform image-uploading

我正在设计一个Web应用程序,后端系统是Express + Parse.Server。目前,我遇到了如何处理在Parse服务器中保存图片的问题。

这是我的ejs文件

<html>

    <head>      
            <title>test</title> 
    </head>
    <body>
    <form action='/pictest' method='post'>
        <h3>test</h3>

        <input type="file" name="pic" id ="pict" >
        <input type='submit' value='Submit'>    
        </form>
    </body>

</html>

这是server.js

router.post('/pictest',function(req,res){
    var pic = req.body.pic;
    console.log(pic);
    if (pic.length > 0) {

      var file = new Parse.File("photo.jpg", pic, "image/png");

       file.save().then(function() {
       console.log("file save success");
       res.end();
       }, function(error) {
       console.log("file save error " + error.message);
        });

    }
    res.send('test');
});

我得到的错误是:

TypeError: Cannot create a Parse.File with that data.
   at new ParseFile (C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\parse\lib\node\ParseFile.js:131:15)
   at C:\Users\Tim\Desktop\example\JavascriftLinkParse\routers\index.js:29:15
   at Layer.handle [as handle_request] (C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\express\lib\router\layer.js:95:5)
   at next (C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\express\lib\router\route.js:131:13)
   at Route.dispatch (C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\express\lib\router\route.js:112:3)
   at Layer.handle [as handle_request] (C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\express\lib\router\layer.js:95:5)
   at C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\express\lib\router\index.js:277:22
   at Function.process_params (C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\express\lib\router\index.js:330:12)
   at next (C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\express\lib\router\index.js:271:10)
   at Function.handle (C:\Users\Tim\Desktop\example
\JavascriftLinkParse\node_modules\express\lib\router\index.js:176:3)

任何帮助都会有所帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将multer与快递

一起使用

并在控制器

var buffer = req.files.pic[0].buffer;
var base64 = buffer.toString('base64');
var name = "pic_name";
var file = new Parse.File(name, { base64: base64 });