Javascript 如何保存ArrayBuffer?
如何在json文件中保存ArrayBuffer?我使用electron-config进行此操作,但在config.json中我找到了“{}”。我尝试将()ArrayBuffer转换为string,但无法将string转换为ArrayBufferJavascript 如何保存ArrayBuffer?,javascript,json,node.js,electron,Javascript,Json,Node.js,Electron,如何在json文件中保存ArrayBuffer?我使用electron-config进行此操作,但在config.json中我找到了“{}”。我尝试将()ArrayBuffer转换为string,但无法将string转换为ArrayBuffer put: function(key, value) { //value = { prop1: <ArrayBuffer>, prop2: <ArrayBuffer> } if (key === undefined
put: function(key, value) {
//value = { prop1: <ArrayBuffer>, prop2: <ArrayBuffer> }
if (key === undefined || value === undefined || key === null || value === null)
return;
var prop1Str,prop2Str;
prop1Str = this.ab2str(value.prop1);
prop2Str = this.ab2str(value.prop2);
var chValue = {prop1:prop1Str, prop2:prop2Str};
config.set(key,chValue);
console.log(value.prop1 === this.str2ab(config.get(key).prop1)); //===> FALSE
},
ab2str: function(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
},
str2ab: function(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
put:函数(键、值){
//值={prop1:,prop2:}
如果(键===未定义| |值===未定义| |键===空| |值===空)
返回;
var prop1Str、prop2Str;
prop1Str=这个.ab2str(值.prop1);
prop2Str=这个.ab2str(值.prop2);
var chValue={prop1:prop1Str,prop2:prop2Str};
config.set(键,chValue);
console.log(value.prop1==this.str2ab(config.get(key.prop1));//=>FALSE
},
ab2str:功能(buf){
返回字符串.fromCharCode.apply(null,新的Uint8Array(buf));
},
str2ab:功能(str){
var buf=新阵列缓冲区(str.length);
var bufView=新UINT16阵列(buf);
对于(变量i=0,strLen=str.length;i
JSON格式中没有ArrayBuffers(只有字符串、数字、布尔值、null
、对象和数组),因此如果要将ArrayBuffer保存为JSON格式,则必须使用其中一种类型(可能是字符串或数字数组)来表示
然后,当您读取JSON时,必须将其转换回ArrayBuffer,与之前的转换相反。JSON格式中没有ArrayBuffers(只有字符串、数字、布尔值、
null
、对象和数组)因此,如果您想在JSON中保存一个ArrayBuffer,那么您必须用其中一种类型(可能是字符串或数字数组)来表示它
然后,当您读取JSON时,您必须将其转换回ArrayBuffer,与之前的转换相反。要保存到磁盘,您应该能够使用普通的节点API将内容写入磁盘。例如:
require('fs').writeFileSync('/path/to/saved/file', Buffer.from(myArrayBuffer));
为了保存到磁盘,您应该能够使用普通的节点API将内容写入磁盘。例如:
require('fs').writeFileSync('/path/to/saved/file', Buffer.from(myArrayBuffer));
工作代码段节点14.xx+
创建输出目录
let rootDir = process.cwd()
console.log("Current Directory"+ rootDir)
let outDir = './out/';
console.log("Out Directory"+ outDir)
if (!fs.existsSync(outDir)){
fs.mkdirSync(outDir);
}else{
console.log("Directory already exist");
}
// Save the raw file for each asset to the current working directory
saveArrayAsFile(arrayBuffer, outDir+ "fileName"+ new Date().getTime()+".png")
保存文件功能
const saveArrayAsFile = (arrayBuffer, filePath)=> {
fs.writeFile(filePath, Buffer.from(arrayBuffer), 'binary', (err)=> {
if (err) {
console.log("There was an error writing the image")
}
else {
console.log("Written File :" + filePath)
}
});
};
工作代码段节点14.xx+
创建输出目录
let rootDir = process.cwd()
console.log("Current Directory"+ rootDir)
let outDir = './out/';
console.log("Out Directory"+ outDir)
if (!fs.existsSync(outDir)){
fs.mkdirSync(outDir);
}else{
console.log("Directory already exist");
}
// Save the raw file for each asset to the current working directory
saveArrayAsFile(arrayBuffer, outDir+ "fileName"+ new Date().getTime()+".png")
保存文件功能
const saveArrayAsFile = (arrayBuffer, filePath)=> {
fs.writeFile(filePath, Buffer.from(arrayBuffer), 'binary', (err)=> {
if (err) {
console.log("There was an error writing the image")
}
else {
console.log("Written File :" + filePath)
}
});
};
为什么需要在JSON中存储二进制数据?您正在向服务器发送HTTP请求吗?如果是这样,最好使用二进制友好的东西,例如多部分/表单数据,而不是JSON。@mscdex不一定是JSON格式的。我只想将ArrayBuffer保存在PC上。你是说将其保存到本地计算机磁盘上的文件中,还是说将其保存在浏览器中,比如通过LocalStorage(本机不支持二进制数据)进行保存?@mscdex我是说将其保存在本地计算机的磁盘上(Electron app)。为什么需要将二进制数据存储在JSON中?您正在向服务器发送HTTP请求吗?如果是这样,最好使用二进制友好的东西,例如多部分/表单数据,而不是JSON。@mscdex不一定是JSON格式的。我只想将ArrayBuffer保存在PC上。你是说将其保存到本地计算机磁盘上的文件,还是说将其保存在浏览器中,比如通过LocalStorage(本机不支持二进制数据)?@mscdex我是说将其保存在本地计算机的磁盘上(Electron app)。我只想将ArrayBuffer保存在本地数据库或文件中。我找不到本地数据库,不在JSON中存储数据。我知道应该如何将ArrayBuffer转换为字符串,但我无法将字符串转换为ArrayBuffer。我只想将ArrayBuffer保存在本地数据库或文件中。我找不到本地数据库,不在JSON中存储数据。我知道应该如何将ArrayBuffer转换为字符串,但我无法将字符串转换为ArrayBuffer。