英文:
Using Web API to download a file
问题
我对Web API模板进行了轻微更改,以使下载工作端点。
[HttpGet(Name = "GetWeatherForecast")]
public FileStream Get()
{
var path = @"C:\Users\user1\download.msi";
return new FileStream(path, FileMode.Open, FileAccess.Read);
}
这是一个100MB的下载文件,当我尝试下载文件时,出现了"内存不足"的问题。
英文:
I took the Web API template and made slight change to working endpoint for the download.
[HttpGet(Name = "GetWeatherForecast")]
public FileStream Get()
{
var path = @"C:\Users\user1\download.msi";
return new FileStream(path, FileMode.Open, FileAccess.Read);
}
It's a 100mb download and I'm getting Out of memory
when I trying download the file.
答案1
得分: 1
要输出一个文件,你应该使用 File
方法:
var f = new FileStream(path, FileMode.Open, FileAccess.Read);
return File(f, "application/octet-stream");
如果你返回一个 FileStream,我猜 ASP.NET 会将其视为 特殊类型,在输出之前尝试对整个对象进行序列化。
英文:
To output a file, you should use the File
method:
var f = new FileStream(path, FileMode.Open, FileAccess.Read);
return File(f, "application/octet-stream");
If you return a FileStream, I guess ASP.NET will consider it as special type, attempting to serialize the entire object before outputing it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论