【问题标题】:Sending image via ajax to multer通过ajax发送图像到multer
【发布时间】:2020-01-30 12:34:16
【问题描述】:

我正在使用 Multer 将图像文件上传到我的节点服务器,它总是返回给我未定义的使用 ajax 发送图像。

阿贾克斯:

image = $("#input-file-img").val()
                const data = new FormData();
                 data.append("image", image);
                 $.ajax({
                    url: '/uploadfile/' + userName,
                    method: 'POST',
                    async: false,
                    processData: false ,
                    contentType: false,
                    data: data
                })

上传.js

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
})
var upload = multer({ storage: storage })

router.post('/uploadfile/:userName', upload.single('image'), async (req, res, next) => {
  let user = await User.findOne({ userName: req.params.userName })
  let file = req.file
  let fileName = file.filename
  let path = variables.kepler + 'uploads/' + fileName
  user.image = path
  await user.save()
  if (!path) {
    const error = new Error('Please upload a file ...')
    error.httpStatusCode = 400
    return next(error)
  }

  if (path) {
    return res.status(200).send({
      status: '200',
      message: 'Operation completed successfully ...',
      data: user,
      path
    })
  }
})

我用控制台检查了图像值,它显示 C:\fakepath\Capture d'écran de 2019-09-19 11-33-59.png'

不胜感激。

【问题讨论】:

    标签: javascript html node.js ajax multer


    【解决方案1】:

    我认为你的服务器端代码很好,如果我修改客户端代码如下,一切正常,我们最终在 /uploads 文件夹中得到图像:

    function base64toBlob(base64, mimeType) {
        const bytes = atob(base64.split(',')[1]);
        const arrayBuffer = new ArrayBuffer(bytes.length);
        const uintArray = new Uint8Array(arrayBuffer);
        for (let i = 0; i < bytes.length; i++) {
            uintArray[i] = bytes.charCodeAt(i);
        }
        return new Blob([ arrayBuffer ], { type: mimeType });
    }
    
    function submitForm() {         
        const imgRegEx = /^data:(image\/(gif|png|jpg|jpeg))/;
        const imageData = $('#input-file-img').attr('src');
        const mimeType = imgRegEx.exec(imageData)[1];
        const blob = base64toBlob(imageData, mimeType);
    
        const fileExt = mimeType.replace("image/", "");
        const fileName = "test-image." + fileExt; // Change as appropriate..
    
        const data = new FormData();
        data.append("image", blob, fileName);
    
        $.ajax({
            url: '/uploadfile/' + userName,
            method: 'POST',
            async: false,
            processData: false ,
            contentType: false,
            data: data
        })
    }
    

    【讨论】:

      【解决方案2】:

      解决了!!!

      通过获取价值

      image = $("#input-file-img").val()
      

      这意味着我将字符串类型作为文件发送

      所以我不得不把它改成

      image = $('#input-file-img')[0].files[0]
      

      一切都很好

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        • 2016-02-03
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 2013-08-10
        • 1970-01-01
        相关资源
        最近更新 更多