Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 读取文件时出现节点内存问题_Javascript_Node.js_Cryptojs - Fatal编程技术网

Javascript 读取文件时出现节点内存问题

Javascript 读取文件时出现节点内存问题,javascript,node.js,cryptojs,Javascript,Node.js,Cryptojs,只是想上传一个文件(PDF)。有人在试图上传65mb的文件时注意到了一个问题,但我注意到当我超过30mb时,VS代码的控制台会出现错误。请查看错误中的堆栈跟踪。我只是想知道有没有其他方法可以避免大文件的内存崩溃?有人提到了溪流,我也试过了,但运气不好。 如果你需要更多的信息,我很乐意提供。谢谢:) 代码: 导出常量加密文件:( fsPath:string, encryptionKey:string, )=>承诺=异步(fsPath,encryptionKey)=>{ 如果(!encryption

只是想上传一个文件(PDF)。有人在试图上传65mb的文件时注意到了一个问题,但我注意到当我超过30mb时,VS代码的控制台会出现错误。请查看错误中的堆栈跟踪。我只是想知道有没有其他方法可以避免大文件的内存崩溃?有人提到了溪流,我也试过了,但运气不好。 如果你需要更多的信息,我很乐意提供。谢谢:)

代码:

导出常量加密文件:(
fsPath:string,
encryptionKey:string,
)=>承诺=异步(fsPath,encryptionKey)=>{
如果(!encryptionKey)抛出新错误(“未找到客户加密密钥”);
让dataBase64='';
const aes=crypto.createCipher('aes-256-cbc',encryptionKey);
const read=createReadStream(fsPath);
const write=createWriteStream(fsPath);
const trans=新变换();
转换=(数据,完成)=>{
dataBase64+=data.toString();
完成(空,数据库64);
}
console.log('dataBase64'+dataBase64)
read.pipe(trans.pipe(aes.pipe(write));
};

正如您所提到的,您只会收到大文件的错误。对于大型缓冲区来说,这似乎是一个内存问题。 我建议您创建流并通过管道执行所有三个操作(读、加密和写)

如果您可以与streams用户共享您的代码片段,这将有助于理解为什么它不起作用的问题


谢谢,

正如您所提到的,您只会收到大文件的错误。对于大型缓冲区来说,这似乎是一个内存问题。
const fs = require('fs');
const crypto = require('crypto');

const encryptFile = async (fsPath, encryptionKey) => {
 if (!encryptionKey) throw new Error('Customer encryption key not found');
 
 const read = fs.createReadStream(fsPath);
 const aes = crypto.createCipher('aes-256-cbc', encryptionKey);
 const write = fs.createWriteStream('output.pdf');
 
 read.pipe(aes).pipe(write);
};

encryptFile('a.pdf', 'dummy-secret-id');
我建议您创建流并通过管道执行所有三个操作(读、加密和写)

如果您可以与streams用户共享您的代码片段,这将有助于理解为什么它不起作用的问题

谢谢

const fs = require('fs');
const crypto = require('crypto');

const encryptFile = async (fsPath, encryptionKey) => {
 if (!encryptionKey) throw new Error('Customer encryption key not found');
 
 const read = fs.createReadStream(fsPath);
 const aes = crypto.createCipher('aes-256-cbc', encryptionKey);
 const write = fs.createWriteStream('output.pdf');
 
 read.pipe(aes).pipe(write);
};

encryptFile('a.pdf', 'dummy-secret-id');
这对我来说很有效,不需要使用转换。您真的需要将数据转换为
base 64



这对我来说很有效,不需要使用转换。您真的需要将数据转换为
base64

我正在尝试管道,但距离不远。我正在努力解决这个问题,但遇到了一些问题,比如:“string类型的参数不可分配给writeablestream类型的参数。”是的,所以您可能会遇到一些小问题。你能把片段贴在这里吗?我们可以看一看。将问题编辑为包含流。我不知道如何像你提到的那样用管道输送水流。我只提到过这样使用它。下面是一些参考示例,看看这些示例是否有用:我认为是因为您在回调中使用空数据调用了
done(null,“”)
,而导致的错误。所以下一个写流进程将抛出异常。我正在尝试管道,但距离不远。我正在努力解决这个问题,但遇到了一些问题,比如:“string类型的参数不可分配给writeablestream类型的参数。”是的,所以您可能会遇到一些小问题。你能把片段贴在这里吗?我们可以看一看。将问题编辑为包含流。我不知道如何像你提到的那样用管道输送水流。我只提到过这样使用它。下面是一些参考示例,看看这些示例是否有用:我认为是因为您在回调中使用空数据调用了
done(null,“”)
,而导致的错误。所以下一个写流进程将抛出异常。try
read.pipe(writer)如果这样做有效,您需要将缓冲区作为done函数中的第二个参数传递。您还可以将加密字符串转换为缓冲区如果这样做有效,您需要将缓冲区作为done函数中的第二个参数传递。您也可以将加密字符串转换为缓冲区。不幸的是,这对我不起作用。pdf格式不正确,无法打开。您是否复制了我添加的相同代码?在您的代码片段中,我看到的问题是,对于读写流,您有相同的
fspath
。是的,文件在我的应用程序中无法正确打开。我意识到您建议的方式是根本不读取文件。零字节。上面的例子对我来说很好。您是否修复了这个
const read=createReadStream(fsPath);const write=createWriteStream(fsPath)?您提到了读写流的相同路径。不幸的是,它对我不起作用。pdf格式不正确,无法打开。您是否复制了我添加的相同代码?在您的代码片段中,我看到的问题是,对于读写流,您有相同的
fspath
。是的,文件在我的应用程序中无法正确打开。我意识到您建议的方式是根本不读取文件。零字节。上面的例子对我来说很好。您是否修复了这个
const read=createReadStream(fsPath);const write=createWriteStream(fsPath)?您提到了读写流的相同路径。
UnknownErrorException {name: "UnknownErrorException", message: "PDFDocument: Stream must have data", details: "Error: PDFDocument: Stream must have data"}
export const encryptFile: (
  fsPath: string,
  encryptionKey: string,
) => Promise<void> = async (fsPath, encryptionKey) => {
  if (!encryptionKey) throw new Error('Customer encryption key not found');
  let dataBase64 = '';
  const aes = crypto.createCipher('aes-256-cbc', encryptionKey);
  const read = createReadStream(fsPath);
  const write = createWriteStream(fsPath);
  
  const trans =  new Transform();
  trans._transform = (data, _, done) => {
    dataBase64 += data.toString();
    done(null, dataBase64);
  }
  console.log('dataBase64 ' + dataBase64)
  read.pipe(trans).pipe(aes).pipe(write);
};

const fs = require('fs');
const crypto = require('crypto');

const encryptFile = async (fsPath, encryptionKey) => {
 if (!encryptionKey) throw new Error('Customer encryption key not found');
 
 const read = fs.createReadStream(fsPath);
 const aes = crypto.createCipher('aes-256-cbc', encryptionKey);
 const write = fs.createWriteStream('output.pdf');
 
 read.pipe(aes).pipe(write);
};

encryptFile('a.pdf', 'dummy-secret-id');