How to save files on local storage received from FormData constructor ? It says "arrayBuffer" is not a function

huangapple go评论57阅读模式
英文:

How to save files on local storage received from FormData constructor ? It says "arrayBuffer" is not a function

问题

我想要保存从客户端收到的文件到我的本地存储。以下是Node.js中的服务器端代码。

app.post('/api/upload/media', (req, res) => {
  var form = new formidable.IncomingForm();
  form.parse(req, (err, fields, file) => {
    if (err) {
      return res.status(400).json({
        message: "Image could not be uploaded"
      })
    }
    if(file){
      console.log(fields)
      // const buffer = Buffer.from(file.arrayBuffer());
      // fs.writeFile("./files/image.png", buffer, (err) => {
      //   console.error(err)
      // })
    }
  })
  res.send(file.name)
});

console.log(file) 打印出这个。

英文:

I want to save file received from client side on my local storage. Below is the server side code in node js.

app.post('/api/upload/media', (req, res) => {
  var form = new formidable.IncomingForm();
  form.parse(req, (err, fields, file) => {
    if (err) {
      return res.status(400).json({
        message: "Image could not be uploaded"
      })
    }
    if(file){
      console.log(fields)
      // const buffer = Buffer.from(file.arrayBuffer());
      // fs.writeFile("./files/image.png", buffer, (err) => {
      //   console.error(err)
      // })
    }
  })
  res.send(file.name)
}); 

console.log(file) prints this.

How to save files on local storage received from FormData constructor ? It says "arrayBuffer" is not a function

答案1

得分: 1

如果您查看控制台中的文件对象,您可以看到一个path属性。这是您的文件在计算机上存储的位置。事实上,formidable在接收到文件后会立即将文件写入磁盘,然后执行.parse()方法的回调函数。

好消息是您可以自定义保存文件的目录,如下所示:

app.post('/api/upload/media', (req, res) => {
  var form = new formidable.IncomingForm();
  form.uploadDir = "/you/custom/upload/folder";
  form.parse(req, (err, fields, file) => {
    if (err) {
      return res.status(400).json({
        message: "Image could not be uploaded"
      });
    }
    if (!file) {
      // 如果没有文件,则抛出错误
    }
    res.send(file.name);
  });
});

重要的部分是这一行。文件将保存在设置了uploadDir的目录中:

form.uploadDir = "/you/custom/upload/directory";
英文:

If you look at the file object in your console, you can see a path property. This is the location where your file is stored on your machine. In facts, formidable write the file to your disk as soon as it receives it, before executing the callback of the .parse() method.

The good news is you can customize the directory where the files are saved this way:

app.post('/api/upload/media', (req, res) => {
  var form = new formidable.IncomingForm();
  form.uploadDir = "/you/custom/upload/folder"
  form.parse(req, (err, fields, file) => {
    if (err) {
      return res.status(400).json({
        message: "Image could not be uploaded"
      })
    }
    if(!file){
      // Throw an error if there is no file
    }
    res.send(file.name)
  })
}); 

The important part is this line. The file will be saved in whatever directory set the uploadDir

form.uploadDir = "/you/custom/upload/directory"

huangapple
  • 本文由 发表于 2023年5月28日 19:00:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定