2016年9月9日 星期五

vue, multipart upload



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
export const doImageUpload = ({ dispatch, state }, file, resizeArr) => {
  const SIGNUP_USER_URL = 'http://localhost:3000/api/v1/upload/upload'

  var fd = new FormData() // eslint-disable-line
  fd.append('file', file)
  console.log('going to post : ' + file.size / 1000 + 'mb')

  var start = new Date().getTime()
  Vue.http.post(SIGNUP_USER_URL, fd, {
    headers: {
      'Content-Type': undefined
    }
  })
  .success(function (result) {
    var returnList = []

    for (let index in resizeArr) {
      var w = resizeArr[index].w
      var h = resizeArr[index].h
      returnList.push(result.fileUrl + '?imageView2/2/w/' + w + '/h/' + h + '/q/100/format/jpg')
    }
    returnList.push(result.fileUrl)
    dispatch('DO_IMAGE_UPLOAD', returnList)
  }).error(function (error) {
    console.log(error)
  })
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



/**
 * Upload floorplan file for the given buildId
 */
 // requiredLogin
router.post("/upload", _pre_form, function(req, res) {
  var files = req.uploadFile;
  var fileLocalPath = files.path;

      console.log(" -------- 3 ")

  fs.readFile(fileLocalPath, function(err, data){
      console.log(" -------- 4 ")
      if(err){
        res.status(httplib.INTERNAL_SERVER_ERROR).json({code: 2021,error: errorMsg(2021, err)});
      }

      console.log(" -------- 5 ")
      var base64Data = data.toString('base64');
      var theFile = new AV.File(files.name, {base64: base64Data});
      theFile.save().then(function(result) {
      console.log(" -------- 6 ")

          res.status(httplib.OK).json({
              fileId: result.id,
              fileName: result.name(),
              mimeType: result.metaData().mime_type,
              fileUrl: result.url()
          })

      },function(err){
        res.status(httplib.INTERNAL_SERVER_ERROR).json({code: 2021,error: errorMsg(2021, err)});
      });
  });

});


2016年9月5日 星期一