我正在尝试使用js
从.net core
应用程序下载文件,并且我希望服务器设置文件扩展名。
不使用File
方法不知道如何设置它:
什么有效
public FileResult DownloadFileAsync() {
FileStream stream = new FileStream(FILEPATH, FileMode.Open, FileAccess.Read);
this.Response.ContentType = "application/octet-stream";
this.Response.ContentLength = stream.Length;
return File(stream, "application/force-download", "data2.txt");
^
}
[我正在努力使它正常工作
public async Task DownloadAsync(long id) {
using (FileStream stream = new FileStream(FILEPATH, FileMode.Open, FileAccess.Read)) {
this.Response.ContentType = "application/octet-stream";
this.Response.ContentLength = stream.Length;
await stream.CopyToAsync(this.Response.Body);
}
}
P.S与第一种情况一样,如何不使用File
方法来设置文件扩展名。response
中还有另一个字段可以设置文件的type
吗?
使用Reponse的content-disposion属性为我工作,在这种情况下,下载的文件将被命名为“ filename.jpg”:
'Content-Disposition':'附件; filename =“ filename.jpg”'