我遇到了一个问题,当我在Windows上将jpg保存为八位字节流时,后续的读取会返回一个大小为0的文件。我可以看到我硬盘上的文件和文件看起来不错,但是读取文件失败了。
以下代码是我用来与文件系统进行交互的代码,其中有一些黑客可以尝试解决此问题。
function downloadFileFromUrl(url, localPath, contentType, successCallback, failureCallback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function () {
if (this.status == 200) {
localPath = encodeURI(localPath);
writePersistantFile(localPath, this.response, true, contentType,
function (fileEntry) {
if (successCallback != undefined) {
successCallback(fileEntry);
}
},
function (error) {
if (failureCallback != undefined) {
failureCallback(error);
}
});
}
};
xhr.send();
}
function createDirectoryRecursively(path, parentDir, callback, errorCallback) {
if (path == null || path.length == 0) {
callback(parentDir);
return;
}
var index = path.indexOf('/');
var dirName = null;
var remainder = null;
if (index <= 0) {
dirName = path;
} else {
dirName = path.substring(0, index);
remainder = path.substring(index + 1);
}
parentDir.getDirectory(dirName, { create: true, exclusive: true }, function (dirEntry) {
self.createDirectoryRecursively(remainder, dirEntry, callback, errorCallback);
}, function (error) {
if (error.code == 12) {
parentDir.getDirectory(dirName, { create: false }, function (dirEntry) {
self.createDirectoryRecursively(remainder, dirEntry, callback, errorCallback);
}, function (error2) {
errorCallback(error);
});
return;
}
errorCallback(error);
});
}
function getFile(fileName, parentDir, callback, errorCallback) {
if (parentDir == null) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fs) {
getFile(fileName, fs.root, callback, errorCallback);
},
function (error) {
});
return;
}
if (fileName.indexOf('/') >= 0) {
var dirName = fileName.substring(0, fileName.lastIndexOf('/'));
createDirectoryRecursively(dirName, parentDir,
function (directory) {
var name = fileName.substring(fileName.lastIndexOf('/') + 1);
getFile(name, directory, callback, errorCallback);
}, errorCallback);
return;
}
parentDir.getFile(fileName, { create: true },
function (fileEntry) {
console.log('obtained file');
callback(fileEntry);
},
function (error) {
if (error.code == 1) {
parentDir.getFile(fileName, { create: false },
function (fileEntry) {
console.log('obtained file');
callback(fileEntry);
},
function (error) {
if (error.code == 12) {
return;
}
errorCallback(error);
});
return;
}
errorCallback(error);
});
}
function readPersistantFile(filePath, successCallback, errorCallback) {
getFile(filePath, null,
function (fileEntry) {
console.log('obtained file');
fileEntry.file(function (file) {
console.log('file size: ' + file.size);
var reader = new FileReader();
reader.onloadend = function () {
console.log("Successful file read");
if (successCallback != undefined) {
successCallback(this.result);
}
};
var extension = filePath.substring(filePath.lastIndexOf('.') + 1);
if (extension == 'json') {
reader.readAsText(file);
} else {
reader.readAsDataURL(file);
}
});
},
function (error) {
console.log('An error occured while getting the file');
console.error(error);
if (errorCallback != undefined) {
errorCallback(error);
}
});
}
function writePersistantFile(filePath, data, isBinary, contentType, successCallback, errorCallback) {
var dataSize = (data.length != undefined) ? data.length : (data.size != undefined)? data.size : 5 * 1024 * 1024;
window.requestFileSystem(LocalFileSystem.PERSISTENT, dataSize, function (fs) {
console.log('file system opened: ' + fs.name);
var lastIndex = filePath.lastIndexOf('/');
var directory = filePath.substring(0, lastIndex);
createDirectoryRecursively(directory, fs.root, function (parentDir) {
console.log('Directory created');
var fileName = filePath.substring(lastIndex + 1);
getFile(fileName, parentDir,
function (fileEntry) {
console.log('obtained file');
fileEntry.createWriter(function (fileWriter) {
console.log('created writer');
if (typeof (data) == 'string') {
fileWriter.write(new Blob([data], { type: contentType }));
} else {
fileWriter.write(data);
}
if (successCallback != undefined) {
successCallback(fileEntry);
}
});
},
function (error) {
console.log('An error occured while getting the file');
console.error(error);
if (errorCallback != undefined) {
errorCallback(error);
}
});
}, function (error) {
console.log('An error occured while saving the file');
console.error(error);
if (errorCallback != undefined) {
errorCallback(error);
}
});
});
}
答案 0 :(得分:0)
似乎不是上面的代码,可以轻松优化,而是传入的文件夹的长度/数量。
正在编写的同一文件长度为36个字符和4个子文件夹。将其更改为单个文件名,例如&#39; page-1.jpg&#39;没有问题地工作。