我正在使用fetch
通过以下方式获取CSV
:
const csv = await fetch('https://www.mysite-com/admin/order?_export_=page%3A1', {credentials: 'include'});
这是`csv'的打印输出:
CSV is: Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]:
{ body:
Gunzip {
_readableState: [ReadableState],
readable: true,
_events: [Object],
_eventsCount: 7,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: true,
_transformState: [Object],
bytesWritten: 0,
_handle: [Zlib],
_hadError: false,
_writeState: [Uint32Array],
_outBuffer:
<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 3e 0a 3c 68 65 61 64 3e 0a 20 20 3c 6d 65 74 61 20 63 68 61 72 73 65 74 3d 22 75 74 66 ... >,
_outOffset: 0,
_level: -1,
_strategy: 0,
_chunkSize: 16384,
_defaultFlushFlag: 2,
_finishFlushFlag: 2,
_nextFlush: -1,
_info: undefined },
disturbed: false,
error: null },
[Symbol(Response internals)]:
{ url: 'https://www.mysite-com/admin/auth/login',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object] } } }
chunkSize
中的16384
似乎与我正在下载的文件大小匹配。但是如何从csv
中检索数据?尝试csv.text()
错误。
更新:
尝试了以下内容:
const csv = fetch('https://www.mysite-com/admin/order?_export_=page%3A1', {credentials: 'include'})
.then(r => r.text)
.catch(err => {
console.log("Error downloading order csv.", err);
});
if (csv) {
fs.writeFile('./tmp/order/order.xls', csv, 'utf8', err => {
console.log("Error saving downloaded order csv.", err);
});
返回了Error saving downloaded order csv null
答案 0 :(得分:1)
fetch
返回对Response
的承诺。在该响应上,您应该能够调用await response.text()
来获取字符串。
答案 1 :(得分:1)
const csv = fetch('https://www.mysite-com/admin/order?_export_=page%3A1', {
credentials: 'include'
})
.then(function(response) {
return response.text;
})
.then(function(resText) {
console.log(resText);
});