I am sending from front end client side a file, in the server side i have something like this:

我正在从前端客户端发送一个文件,在服务器端我有如下内容:

{ name: 'CV-FILIPECOSTA.pdf',
  data: <Buffer 25 50 44 46 2d 31 2e 35 0d 25 e2 e3 cf d3 0d 0a 31 20 30 20 6f 62 6a 0d 3c 3c 2f 4d 65 74 61 64 61 74 61 20 32 20 30 20 52 2f 4f 43 50 72 6f 70 65 72 ... >,
  encoding: '7bit',
  mimetype: 'application/pdf',
  mv: [Function: mv] }

what i need is to create the file maybe based on that buffer that is there, how can i do it? i already searched a lot and didn't find any solution.

我需要的是创建一个文件也许基于那个缓冲区,我怎么做呢?我已经找了很多,没有找到任何解决办法。

this is what i tried so far:

这是我到目前为止所尝试的:

router.post('/upload', function(req, res, next) {
  if(!req.files) {
    return res.status(400).send("No Files were uploaded");
  }
  var curriculum = req.files.curriculum; 
  console.log(curriculum);
  curriculum.mv('../files/' + curriculum.name, function(err) {
    if (err){
      return res.status(500).send(err);      
    }
    res.send('File uploaded!');
  });
});

1 个解决方案

#1


3

You could use Buffer available with NodeJS:

可以使用NodeJS提供的缓冲区:

let buf = Buffer.from('this is a test');
// buf equals <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>

let str = Buffer.from(buf).toString();
// Gives back "this is a test"

Encoding could also be specified in the overloaded from method.

也可以在重载的from方法中指定编码。

const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
// This tells that the first argument is encoded as a hexadecimal string

let str = buf2.toString();
// Gives back the readable english string
// which resolves to "this is a tést"

After you have data available in the readable format, you could store it using the fs module in NodeJS.

在以可读格式提供数据之后,可以使用NodeJS中的fs模块来存储数据。

fs.writeFile('myFile.txt', "the contents of the file", (err) => {
  if(!err) console.log('Data written');
});

So, after converting the buffered input to string, you need to pass the string into the writeFile method. You could check the documentation of the fs module. It will help you better understand things.

因此,在将缓冲输入转换为字符串之后,需要将字符串传递给writeFile方法。您可以查看fs模块的文档。它将帮助你更好地理解事物。

更多相关文章

  1. 11、javascript中字符串常用操作总结、JS字符串操作大全
  2. 从占用转义字符的字符数组创建字符串
  3. javascript如何取字符串中的数字
  4. JavaScript中,提取子字符串方法:Slice、Substring、Substr的比较
  5. java 如何获取动态网页内容,返回字符串
  6. NodeJS - 解析JSON(只有字符串或数字)
  7. 如何在JavaScript中对字符串排序
  8. JSON.parse(xhr.responseText)意外的字符串错误
  9. 如何在url中将特殊字符作为查询字符串传递

随机推荐

  1. PHP数组合并之array_merge和数组相加
  2. PHP操作数据库
  3. php代码连不上mysql
  4. php final关键字的应用
  5. php错误屏蔽
  6. PHP开发APP接口全过程(二)
  7. 30 个 php 操作 redis 常用方法代码示例
  8. 用php生成HTML文件的类
  9. PHP开发APP接口全过程(一)
  10. php中base64加密解密函数实例(附代码)