【发布时间】:2020-11-21 10:42:23
【问题描述】:
在 js 中我正在运行这段代码
let str = new TextDecoder("utf8").decode(new Uint8Array(reader.result));
let data = {
name:file.name,
size: args.loaded,
contentsize:str.length,
type:file.type,
content:str
};
let res = await $.post({
url:"/api/files",
data:JSON.stringify(data),
processData: false,
contentType: "application/json",
})
reader.result 是
的输出reader.readAsArrayBuffer(file);
然后在 php 中提取
$new["actual"] =strlen($new["content"]);
$new["content"] = new MongoDB\BSON\Binary($new["content"] ,MongoDB\BSON\Binary::TYPE_GENERIC);
$result = IO::$db->files->insertOne($new);
然后生成一个 db 文档
{
"name" : "test.pdf",
"size" : NumberInt(128454),
"contentsize" : NumberInt(122693),
"type" : "application/pdf",
"actual" : NumberInt(215693)
}
但是,当我将文件的内容移回文件系统时,文件已损坏并且无法打开。 我已经运行了 VSCode 附带的文件比较器,并说 DB 内容和原始内容之间存在 0 差异,所以我只能认为问题在于文件大小和读取内容的 3kb 差异,但是什么或者为什么逃脱我
注意似乎只是二进制文件
【问题讨论】:
标签: javascript php ajax pdf