首页 > 解决方案 > 如何从asp.net核心下载带有角度的fileStream

问题描述

大家好,我想下载 angular 7 的文件,我的后端是 .net 核心,这是我的后端代码:

[HttpPost]
public FileStream DownloadPack([FromBody]string gridplusSelectedData)
{
try
{
var updateData = JsonConvert.DeserializeObject<UpgradeViewModel>(gridplusSelectedData);
var getdateofGettingFiles = DateTime.Now.GetHashCode();
var selectedPPatch = @"G:\PackTemp\" + getdateofGettingFiles;
var mngfile = new CreatePatchCustomForCustomer();
var result = mngfile.CreatePatchFolder(updateData.GeneralInformation.GeneralStatus, updateData.GeneralInformation.AllDependencies, updateData.GeneralInformation.PriorityList, selectedPPatch);
// return new File(result, System.Net.Mime.MediaTypeNames.Application.Octet, "PatchDirectory.zip") ;
// return File(result, System.Net.Mime.MediaTypeNames.Application.Zip , "PatchDirectory.zip");

return new FileStream(result, FileMode.Open, FileAccess.Read);
}
catch (Exception ex)
{

return null;
}
}

这是我的前端代码

public downloadPatch() {
  let previousData = this.configService.finalObjForDownloadPack
     this.configService.post("PackPatchManager/DownloadPack", previousData,"zip").subscribe(blob => {
     saveAs(blob, 'PatchDirectory.zip', {
        type: 'text/plain;charset=windows-1252' // --> or whatever you need here
     });
  });

}
here is my front end Service
  public post(url, body, mode?): Observable<any> {
    let httpOptions
    if (mode == "zip") {
      httpOptions = {
        headers: new HttpHeaders({'Content-Type': 'application/json', 'responseType': 'blob' })
      }
    }
    else {
      httpOptions = {

        headers: new HttpHeaders({ 'Content-Type': 'application/json' })

      }
    }

    return this.http.post(this.serverAdress + url, body, httpOptions);
  }

然后我收到此错误:错误图片

我的要求和回应:铬合金

我的http post请求出错而不是成功

标签: angular.net-coreangular-httpclient

解决方案


推荐阅读