android - nodejs 是如何处理post来的图片 使其放入 /public/images 里的?
PHP中文网
PHP中文网 2017-04-17 16:44:27
0
6
803
PHP中文网
PHP中文网

认证0级讲师

reply all(6)
洪涛

express

form add this: enctype="multipart/form-data"

input(type="file")

Quote

var multiparty = require('connect-multiparty')
var multipartMiddleware = multiparty();

Routing
app.post('/movie/save',multipartMiddleware,Movie.savePoster,Movie.save)



exports.savePoster = function(req, res, next) {
  var posterData = req.files.uploadPoster
  var filePath = posterData.path
  var originalFilename = posterData.originalFilename

  if (originalFilename) {
    fs.readFile(filePath, function(err, data) {
      var timestamp = Date.now()
      var type = posterData.type.split('/')[1]
      var poster = timestamp + '.' + type
      **//将文件保存到特定的目录**
      var newPath = path.join(__dirname, '../../', '/file/images/' + poster)

      fs.writeFile(newPath, data, function(err) {
        // 自定义
        req.poster = poster
        next()
      })
    })
  }
  else {
    next()
  }
}

最后可以参考:http://www.imooc.com/learn/197中关于上传海报的章节视频,这个是过期的,后面一章有升级的方法
巴扎黑

node express has an upload plug-in that automatically parses, you just need to provide a directory and it’s very simple

小葫芦

Use formidable to parse uploaded images, simple and clear. Just check out the API of this module on npm

巴扎黑

express uses connect-busboy to process the multipart/*data

of image uploads

Here is a sample code demo

阿神

It is recommended to use formidable. After my practice in the past few days, the API design of formidable is more reasonable and facilitates more customized operations. The most important thing is that it supports multiple file uploads.
connect-busboy does not have the above advantages and does not support multipart

小葫芦
 app.post('/ava',multipart(),function(req, res){
        var filename = req.files.avatar.originalFilename || path.basename(req.files.avatar.path);
        var targetPath = pathname + '/image_repository/avatar/' + filename;
        fs.createReadStream(req.files.avatar.path).pipe(fs.createWriteStream(targetPath));
        var _url = '/avatar/' + filename;
        console.log(_url);
        console.log(targetPath);
        var _name = req.session.user;
        //用module方法保存数据
        User.update({name:_name},{$set:{avatar:_url}},function(err){
            if (err) throw err;


        });
        res.json({
            codetype : 200,
            msg:{url:'http://' + req.headers.host + '/' + filename},
            url:_url
        });
        //用entity方法保存数据,效果相同
        //User.findOne({name:_name},function(err, doc){
        //    if (err) throw err;
        //    if (doc){
        //        doc.set({avatar:targetPath});
        //        doc.save();
        //    }else{
        //        console.log('no user');
        //    }
        //});
        //res.json({
        //    codetype : 200,
        //    msg:{url:'http://' + req.headers.host + '/' + filename},
        //    url:_url
        //});
        //var _img = req.files;
        // console.log(_img);
        //res.json(_img);
    });

The image is passed in from the front end using the formdata object, and the stream is used to copy the image to the folder you want

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template