Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Angular 2保存Excel文件_Asp.net_Excel_Angular_Download - Fatal编程技术网

Asp.net Angular 2保存Excel文件

Asp.net Angular 2保存Excel文件,asp.net,excel,angular,download,Asp.net,Excel,Angular,Download,我正试图将一个Excel文件从服务器保存到客户端PC,但它被搞砸了 当我向服务器请求我的文件时,请求主体如下所示: ��ࡱ�>�� &���� 我想这是正常的,因为excel是二进制格式的。我正在使用文件保护程序插件来保存我的文件,目前我的CSV和ASCII文件运行良好。 这是我要下载的功能: downloadFile(filePath: string, name: string): void{ this.dataImportService.downloadFile(file

我正试图将一个Excel文件从服务器保存到客户端PC,但它被搞砸了

当我向服务器请求我的文件时,请求主体如下所示:

��ࡱ�>�� &����

我想这是正常的,因为excel是二进制格式的。我正在使用文件保护程序插件来保存我的文件,目前我的CSV和ASCII文件运行良好。 这是我要下载的功能:

downloadFile(filePath: string, name: string): void{
        this.dataImportService.downloadFile(filePath)
            .then(data => {
                this.headers = data.headers;
                let content = this.headers.get('content-type');
                var blob = new Blob([data._body], { type: content });
                importedSaveAs(blob, name);
            });
    }
我做错了什么或者我可以改进什么? 提前谢谢你的帮助

编辑:这是我的服务器代码:

[HttpGet]
        public void downloadFile(string path)
        {
            try
            {
                string extension = Path.GetExtension(path);
                string fileName = Path.GetFileName(path);

                HttpResponse response = System.Web.HttpContext.Current.Response;

                response.AppendHeader("Content-Disposition", "attachment; filename="+fileName);
                response.AddHeader("Content-Type", Utils.MimeTypesConverter.GetMimeType(extension));

                response.TransmitFile(path);
                Response.End();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return;
            }

        }
和mydataImportService.ts

downloadFile(filePath: string): any{
    return this.http.get(SERVICE_URL + 'DataImport/downloadFile?path=' + filePath)
      .toPromise()
      .then(response =>{ 
        return response;
        })
      .catch(this.handleError);
  }
来自服务器的我的http响应:


您必须将包含HTTP头的响应映射到Blob本身。 所以,将代码更改为一个可观察的映射,以转换响应

downloadFile(filePath: string): Observable<any> { 
  return this.http.get(
  `${SERVICE_URL}DataImport/downloadFile?path=${filePath}`, 
  {responseType: ResponseContentType.Blob }
) 
.map(res:Response => res.blob()); 
}
下载文件(文件路径:字符串):可观察{ 返回this.http.get( `${SERVICE\u URL}数据导入/下载文件?路径=${filePath}`, {responseType:ResponseContentType.Blob} ) .map(res:Response=>res.blob()); }
顺便说一句,您应该标记服务器代码的语言。它是ASP.NET吗?@JGFMK是的,它是我们的。感谢您与我一起付出的辛勤工作!谢谢你的帮助!还有一些其他的迹象。响应的内容长度为167字节。。。对电子表格来说太短了。当您查看CSV文件的响应头时,它已将传输编码分块。。最初在响应头中缺少的。