[英]How to convert binary to downloadable file extension?
我有一個來自后端的二進制響應,其中包含文件名和要下載的類型。 這是我從后端響應收到的代碼:
01 00 00 00 78 02 00 00 6c 02 00 00 91 16 a2 3d ....x...l....... 9d e3 a6 4d 8a 4b b4 38 77 bc b1 b7 01 00 00 00 ...M.K.8w....... 00 00 00 00 00 00 00 00 00 00 00 00 dc 07 07 00 ................ 05 00 06 00 0c 00 30 00 2f 00 58 01 0f e5 52 9a ......0...X...R. 01 00 00 00 78 02 00 00 6c 02 00 00 91 16 a2 3d ....x...l....... 9d e3 a6 4d 8a 4b b4 38 77 bc b1 b7 01 00 00 00 ...M.K.8w....... 00 00 00 00 00 00 00 00 00 00 00 00 dc 07 07 00 ................ 05 00 06 00 0c 00 30 00 2f 00 58 01 0f e5 52 9a ......0...X...R.
上面的 output 是:“@@”,我想寫入類型 (xlsx,csv,txt),具體取決於從后端接收的類型並將其下載到我的本地。
在我的服務文件中:
public downloadFile() {
const url = 'http://localhost:3300/star';
return this.http.get(url, {
headers: new HttpHeaders({
'Authorization': 'Basic ' + "token",
'Content-Type': 'application/octet-stream',
}), responseType: 'blob'
}).pipe(
tap(
data => console.log('You received data'),
error => console.log(error)
)
);
在組件文件中:
this.service.downloadFile().subscribe((res) => {
//res value is : Blob {size: 519, type: "text/html"}
// Using file-saver to save the file
saveAs(res, `demo.xlsx`);
}, error => {
console.log(error);
});
我不知道從客戶端到底該做什么,以及后端需要做什么才能使其正常工作。 我在某處讀到 base64 不建議從服務器發送,因為文件大小變得巨大。
有一點很清楚,我可能會從后端獲得巨大的文件大小數據,例如,我可能會收到大小為 100 MB 的 xlsx 文件的二進制文件。
如何處理從客戶端下載? 可以批量下載
請在這方面提供幫助。 謝謝你。
/* TODO: replace the blob content with your byte[] */
const blob = new Blob([yourBinaryDataAsAnArrayOrAsAString], {type: "application/octet-stream"});
const fileName = "fileNAme.fileExtension";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.