问题1:
通过FileReader的readAsArrayBuffer读取二进制文件,然后将其上传到后端,存入到Mongodb中,前端打印出来ArrayBuffer确实有数据,为什么存到Mongodb后就没有数据尼?
if(!isinArray(alreadychunks,curindex)) {
let tempcell = {
data: null,
n: 0,
file_id: summary,
username: username,
length: 0
};
tempcell.length = end - start;
filder[curindex] = new FileReader();
filder[curindex].readAsArrayBuffer(file.slice(start, end));
filder[curindex].onload = function () {
tempcell.n = curindex;
tempcell.data = filder[curindex].result
//chunk块上传
chunksarrpromise.push(uploading("chunks", tempcell, chunksurl));
}
}
问题2:
我的需求简单的抽象为:
从浏览器端对用户选择的文件进行切分上传,存入到Mongodb中,在Node.js环境下,
进行文件的下载合并,现在的问题是如何将两边的编码问题保持一致。具体描述为:
(1)如果用户要上传的是文本文件(内容包含汉字),
浏览器端我采用 filder[curindex].readAsText(file.slice(start, end),"gb2312")读取上传
Node.js端直接采用如下代码写入文件:
let req = http.request(options, function (res) {
res.on("data", function (chunk) {
let myoption = {
flags: "r+",
start: i * obj.chunkSize
};
let writeStream = fs.createWriteStream(obj.filename, myoption);
writeStream.write(chunk)
(2)对于图片和媒体类文件,我要怎么实现两边编码格式统一啊,即保证浏览器那边上传后,
Node.js端可以保证正确下载文件可以打开。我尝试过ReadASDataUrl,但是在这边解码base6
如何还原媒体类文件?解码base64后,媒体类文件无法打开了(我目前试了图片)
希望各位码友不吝赐教