我正在尝试使用appjs 保存一个JPG文件..我使用的代码来自示例here。如您所见,我只是将 fileContent 传递给nodejs的 writeFile()。它有效,但生成的JPG图像无法打开(IrfanView显示错误“不是JPEG文件:以0xc3 0xbf开头”。我错过了什么?编码?
这是我用于拖放和保存的完整代码。非常简单: -
addEventListener('app-ready', function(e){
var fs = require('fs');
var readEnd = function(progressEvent) {
console.log('readEnd',progressEvent,this);
var fileReader = this;
var fileContent = fileReader.result;
var fileName = fileReader.file.name;
// Note you can not retreive file path, for security reasons.
// But you are not supposed to need it, you already have the content ;)
fs.writeFile("test.jpg", fileContent, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
console.log('readEnd:');
}
var readFile = function(file) {
var reader = new FileReader();
reader.file = file; // We need it later (filename)
reader.addEventListener('loadend', readEnd, false);
reader.readAsBinaryString(file);
}
var drop = function(event) {
event.preventDefault();
var dt = event.dataTransfer;
var files = dt.files;
for (var i = 0; i<files.length; i++) {
var file = files[i];
readFile(file);
}
}
window.addEventListener("drop", drop);
window.dispatchEvent(new Event('app-done'));
});
以下是包含原始JPG和生成的JPG的ZIP文件:http://ge.tt/4iYy9Kk/v/0?c