[英]How to serve file with specific extension
我正在嘗試使用.net core
應用程序中的js
下載文件,並且我希望服務器設置文件擴展名。
我不知道如何在不使用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);
}
}
PS如何在不使用第一種情況下的File
方法的情況下設置文件擴展名。 response
中是否還有另一個字段可以設置文件的type
?
使用 Reponse 的 content-disposion 屬性為我工作,在這種情況下,下載的文件將被命名為“filename.jpg”:
'內容處理':'附件; 文件名="文件名.jpg"'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.