使用NestJS编写blob内容的方法是什么?

时间:2019-07-03 14:48:04

标签: node.js fs nestjs

从OpenKM下载文件后,我尝试使用fs保存文件,但文件已损坏,丢失了什么?

const fileContent = await this.getFileFromOpenKM('templates/sample.odt');
const buffer = Buffer.alloc(fileContent.length, fileContent);
fs.writeFile(path.join(directoryPath, 'sample.odt'), fileContent, (err) => {
    if (err) throw err;
    console.log('The file has been saved!');
});

下面是我从OpenKM下载文件内容的方式:

async getFileFromOpenKM(uuid: string): Promise<string> {
var URI = "http://localhost:8080/OpenKM/services/rest/document/getContent?docId=" + uuid;

return await this.http.get(URI, { headers: this.headersRequest, responseType: 'blob' })
    .pipe(
        map(response => response.data),
        catchError(err => {
            this.logger.error(`[${OpenKMService.name}]: ${err}`);
            return err;
        }),
    ).toPromise();
}

1 个答案:

答案 0 :(得分:0)

似乎fs更改了文件的内容。继续前进,我已将其替换为wget,如下所示:

 wget({
    url: process.env.OPENKM_DIRECT_LINK + templatePath,
    dest: filePath,
    headers: this.headersRequest
 },
    function (error, response, body) {
       if (error)
          this.logger.error(error);
    }
 );