我使用此代码(假设已经建立了与数据库的连接):
const fs = require('fs');
let docUnid = "F09C0DB42276F208C1258513005722D1";
let sFilename = fileSamplePdf.pdf; // !!! Does not work for reading after attachment !!!
// let sFilename = fileSampleTxt.txt; !!! Work for reading after attachment !!!
let buffer = fs.readFileSync("/tmp/" + sFilename);
const writable = await database.bulkCreateAttachmentStream({});
writable.on('error', e => {
console.error("Error on write File", e);
});
writable.on('response', response => {
console.log("File " + sFilename + " saved to doc " + docUnid);
});
let offset = 0;
const writeRemaining = () => {
if (error) {
return;
}
let draining = true;
while (offset < buffer.length && draining) {
const remainingBytes = buffer.length - offset;
let chunkSize = 16 * 1024;
if (remainingBytes < chunkSize) {
chunkSize = remainingBytes;
}
const chunk = new Uint8Array(
buffer.slice(offset, offset + chunkSize),
);
draining = writable.write(chunk);
//draining = writable.write(buffer.slice(offset, offset + chunkSize)); !!! Does not work, generate a GrpcError !!!
offset += chunkSize;
}
if (offset < buffer.length) {
writable.once('drain', writeRemaining);
} else {
writable.end();
}
};
writable.file({
unid: docUnid,
fileName: sFilename,
});
writeRemaining();
无论文件大小如何(从 1.0.3 版开始),它都能完美运行,并且文件附加到“$File”字段中的文档(大小文件是正确的):
Field Name: $FILE
Data Type: Attached Object
Data Length: 57 bytes
Seq Num: 100
Dup Item ID: 0
Field Flags: ATTACH SIGN SEAL SUMMARY
Object Type: File
Object ID: 00000266
Object Length: 486481
File Name: fileSamplePdf.pdf
Flags:
Host: UNKNOWN
Compression Type: NONE
Encoding Type:
File Attributes: RW PUBLIC
File Size: 486481
File Created: 15/04/2020 16:56:08
File Modified: 15/04/2020 16:56:08
但是在附件之后,如果附件的类型不是txt(例如:jpg,pdf,...),那么即使从Notes客户端也无法读取,这会产生读取错误。
我比较了一个 txt 类型文件和另一个文件的 $File 字段的属性,除了文件的属性之外没有其他区别。
我正在使用带有 IBM Domino (r) Server(64 位)、Release 10.0.1FP3 和 AppDevPack 1.0.3 的 Windows 系统。
预先感谢您的帮助