在angular 7中下载excel文件
嘿,在努力从下面的服务器下载excel之后,我发现这是一个非常简单的解决方案。但是API方面,他们只是读取路径并发送文件。 如何区分文件类型 如果服务器文件位于项目目录或服务器中,我们希望直接下载excel或任何文件。添加了以下仅适用于excel的实现 API(.net):在angular 7中下载excel文件,excel,angular,file,download,Excel,Angular,File,Download,嘿,在努力从下面的服务器下载excel之后,我发现这是一个非常简单的解决方案。但是API方面,他们只是读取路径并发送文件。 如何区分文件类型 如果服务器文件位于项目目录或服务器中,我们希望直接下载excel或任何文件。添加了以下仅适用于excel的实现 API(.net): public ActionResult Download() { string fileName = WebConfigurationManager.AppSettings["filename"];
public ActionResult Download()
{
string fileName = WebConfigurationManager.AppSettings["filename"];
var filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + fileName);
if (System.IO.File.Exists(filePath))
{
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
else
{
var response = new WebServiceStatus()
{
code = -1,
data = null,
message = "File is Not available"
};
var data = JsonConvert.SerializeObject(response);
return HandleTrivialHttpRequests(data);
}
}
角度V7
//声明
headers: HttpHeaders;
options: any;
//构造函数或u可以用于特定方法
this.headers = new HttpHeaders({ 'Content-Type': 'application/json' });
this.options = {
observe: 'response',
headers: this.headers,
responseType: 'arraybuffer'
};
//服务请求:
this.httpClient.post('http://localhost:8080/api/report', this.data,
this.option)
.pipe(
catchError(err => this.handleError(err))
).subscribe(response => {
Helper.exportExelFile(response, 'FileName');
});
//在一个类的组件或助手函数中,我使用了helper
也可以在其他地方重用的功能
import * as FileSaver from 'file-saver';
function exportExelFile(data, filename) {
const blobValue = new Blob([data['body']], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
FileSaver.saveAs(blobValue, filename + '.' + FileType.EXCEL);
}
export const Helper = {
exportExelFile
};
这里有问题吗?或者它是否回应了之前的一个问题?如果回答了,它应该作为该问题的答案发布。这里有问题吗?或者它是否回答了之前的问题?如果回答了,则应将其作为该问题的答案发布。