[英]Saving a file on the client side of my React application
我正在嘗試在我的React應用程序的客戶端上保存文件。 數據是從我的API控制器(稱為DownloadDocument)獲得的。 我要從我的DownloadDocument方法返回FileSreamResult。 FileStreamResult讀取數據並將其寫入react API中的響應中。 網絡選項卡得到響應,但是當我記錄響應時,我得到的響應為null。 我正在使用文件保護程序來嘗試保存文件。 https://www.npmjs.com/package/file-saver 。
是否有人對問題是什么或是否有更好的解決方法有任何建議?
我在控制器中的動作:
我遇到的問題是我對react api的響應返回了null。
[HttpGet("{documentId}")]
[AcceptVerbs("OPTIONS", "GET")]
[AllowAnonymous]
public async Task<IActionResult> DownloadDocument(int documentId)
{
if (documentId != 0)
{
var document = await service.DownloadDocumentAsync(documentId);
var documentResponse = File(document, "application/octet-stream");
return documentResponse;
}
return BadRequest("Document id is not valid");
}
反應應用程序。
api.indexes.downloadDocument(clones)
.then(response=>{
console.log(response)
let FileSaver = require('file-saver');
let blob = new Blob([response], {type: "application/octet-stream"});
let filename ="testdownload"
FileSaver.saveAs(blob, filename)
謝謝您的幫助。
我需要將其添加到標題中。 responseType:“ blob”
這篇文章很好地解釋了https://medium.com/@fakiolinho/handle-blobs-requests-with-axios-the-right-way-bb905bdb1c04
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.