簡體   English   中英

使用請求js損壞的文件

[英]Corrupted file using request js

我正在使用請求js下載文件。

function requ(){
    const options = {
        uri: `api/tasks/${id}/attachments/${attachmentId}`
    }
    return rp.get(options)
 }

我的問題是:

為什么用管道傳遞給像requ()。pipe(res)這樣的“ res”,並使用“ send”返回上述請求的結果

requ().then((result)=>{
    //here result is the file's representing string
    res.send(result)

})

別?

const fs = require('fs');
requ().then((result) => {
    //here result is the file's representing string
    const path = __dirname + '/tempFiles' + Date.now(); // a temporary file to send it 
    fs.writeFile(path, result, function(err) {
        if(err) throw err;
        return res.sendFile(path);
    })
});

閱讀有關fs的更多信息, 鏈接2

我的文件已損壞,因為請求將響應正文轉換為utf8。 使用方法:

const options = {
    uri: `api/tasks/${id}/attachments/${attachmentId}`,
    encoding:null
}

解決了問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM