Home > Net >  Vue. Js axios download file can't open [urgent] how to solve
Vue. Js axios download file can't open [urgent] how to solve

Time:01-12

TXT file can be opened after the download
Images and PDF, etc all can't,

Vue. Js code

 
OnDownload (docId, docName) {
Return new Promise (()=& gt; {
This. DeviceTechDocSvc. DownloadDeviceTechDoc (docId). Then ((resp)=& gt; {
If (resp. Status==200) {
Let the blob=new blob ([resp. Data], {
Type: resp. Headers [" the content-type "],//PDF 'application/octet - stream; Charset=utf-8 '
});
If (window. The navigator & amp; & Window. The navigator. MsSaveOrOpenBlob) {
//compatible with IE
Window. The navigator. MsSaveOrOpenBlob (blob, docName);//you can customize the file name and suffix
} else {
//compatible with Google and fireFox
Var a=document. The createElement method (" a ");
Document. The body. The appendChild (a);
A.s tyle="display: none;"
Var url=window. Url. CreateObjectURL (blob);//create the url
A.h ref=https://bbs.csdn.net/topics/url;
A. d. ownload=docName;
A.c lick ();
A.r emove ();
Window. URL. RevokeObjectURL (URL);//release the url
}
}
});
});
},


The back-end code

///& lt; summary> 
///document download
///& lt;/summary>
///& lt; Param name="docId" & gt;
///& lt; Returns>
[HttpPost (" {docId} ")]
Public async Task The Download (Guid docId)
{
Try
{
The dynamic model=new ExpandoObject ();
Model. The DocId=DocId;
Var entity=this. Logic. Get (model);
If (entity!=null)
{
//file real address
The string fileName=${webHostEnvironment. ContentRootPath} \ \ "uploadDeviceTechDocs \ \ {entity. DeviceId} \ \ {entity. DocUri}";
The FileInfo the FileInfo=new the FileInfo (fileName);
If (the fileInfo. The Exists)
{
FileStream FileStream=the fileInfo. OpenRead ();
Return new FileStreamResult (fileStream, entity. MimeType);
}
}
return null;
}
The catch (BusinessLogicLayerException ex)
{
Throw new Exception (ex. Message);
}
}
  • Related