让我们假设下载所选文件的控制器:
public FileResult Download( string f ) {
Stream file = MyModel.DownloadFiles( f );
return File( file, "application/octet-stream", (file as FileStream).Name );
}
and MyModel包含
public static Stream DownloadFiles(string file){
return new FileStream(file, FileMode.Open, FileAccess.Read);
}
如果我使用using
控制器中的关键字,则会抛出异常:Cannot access closed file
.
好吧,我想确保下载的文件将被处理(我不知道是否可能,如何做到这一点)或不?
Thanks
Controller.File方法使用文件流结果里面的类,已经包含using keyword
protected override void WriteFile(HttpResponseBase response) {
// grab chunks of data and write to the output stream
Stream outputStream = response.OutputStream;
using (FileStream) {
byte[] buffer = new byte[_bufferSize];
while (true) {
int bytesRead = FileStream.Read(buffer, 0, _bufferSize);
if (bytesRead == 0) {
// no more data
break;
}
outputStream.Write(buffer, 0, bytesRead);
}
}
}
https://github.com/ASP-NET-MVC/ASP.NET-Mvc-3/blob/master/mvc3/src/SystemWebMvc/Mvc/FileStreamResult.cs
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)