使用asp.net core 2.1对8号角的文件进行降级处理时,总会给出损坏的文件

问题描述 投票:0回答:2

我在我的项目中使用asp.net core 2.1,在客户端使用angular 8,但是无法为downolad文件提供功能,而下载该文件时只能下载.txt文件,而无法下载.pdf,.excell,图片等。下面是我的代码。

downloadFiles(filename: any) {   
    return this.http.get(this.myAppUrl + 'api/Download/' + filename)
    .catch(this.errorHandler)
}

//after getting response data from server and passing to the method makeDownloadFiles only 
//downloded corrupted file.

makeDownloadFiles(data: any) {
    debugger;
    const blob = new Blob([data]); 
    const url = window.URL.createObjectURL(blob);
    window.open(url);
}

//and server code is 

[HttpGet]
[Route("api/Download/{filename}")]
public FileResult Download(string filename){
    try {
        string extension = filename.Substring(filename.IndexOf('.') + 1);
        string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/files", filename);
        byte[] fileBytes = System.IO.File.ReadAllBytes(path);
        string fileName = filename;
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    } catch (Exception ex){
        return null;
    }
}   
c# angular8 asp.net-core-2.1
2个回答
-1
投票

只需尝试一下。您无需调用api /下载文件的api。只需获取FileUrl并调用下面的方法即可。

 downloadFile(fileNameUrl: any) {

        var filename: string[] = fileNameUrl.split('\\');
        this.getImage(fileNameUrl).subscribe((data: Blob) => {
            debugger;
            let item = filename[filename.length - 1];

            let checkFileType = item.split('.').pop();
            var fileType: any;
            if (checkFileType == "txt") {
                fileType = "text/plain";
            }
            if (checkFileType == "pdf") {
                fileType = "application/pdf";
            }
            if (checkFileType == "doc") {
                fileType = "application/vnd.ms-word";
            }
            if (checkFileType == "docx") {
                fileType = "application/vnd.ms-word";
            }
            if (checkFileType == "xls") {
                fileType = "application/vnd.ms-excel";
            }
            if (checkFileType == "xlsx") {
                fileType = "application/vnd.ms-excel";
            }
            if (checkFileType == "png") {
                fileType = "image/png";
            }
            if (checkFileType == "jpg") {
                fileType = "image/jpeg";
            }
            if (checkFileType == "jpeg") {
                fileType = "image/jpeg";
            }
            if (checkFileType == "gif") {
                fileType = "image/gif";
            }
            if (checkFileType == "csv") {
                fileType = "text/csv";
            }
            if (checkFileType == "amr") {
                fileType = "AMR-WB";
            }
            var blob = new Blob([data], { type: fileType })
            const blob1: Blob = data;
            const fileName1: string = item;
            const objectUrl: string = URL.createObjectURL(blob);
            const a: HTMLAnchorElement = document.createElement('a') as HTMLAnchorElement;

            a.href = objectUrl;
            a.download = fileName1;
            document.body.appendChild(a);
            a.click();

            document.body.removeChild(a);
            URL.revokeObjectURL(objectUrl);
        })
        }

  getImage(imageUrl: string): Observable<Blob> {
    return this._http.get(imageUrl, { responseType: 'blob' });
  }

0
投票

一旦我在对get的调用中指定了类型,而对new Blob的调用则您的代码运行良好:

downloadFiles(filename: any) {   
    return this.http.get(this.myAppUrl + 'api/Download/' + filename, { responseType: 'blob'})
    .catch(this.errorHandler)
}

makeDownloadFiles(data: any) {
    debugger;
    const blob = new Blob([<any>data], {type: 'application/octet-stream'}); 
    const url = window.URL.createObjectURL(blob);
    window.open(url);
}
© www.soinside.com 2019 - 2024. All rights reserved.