Sna*_*yes 3 c# stream asp.net-mvc-3
让我们假设下载所选文件的控制器:
public FileResult Download( string f ) {
Stream file = MyModel.DownloadFiles( f );
return File( file, "application/octet-stream", (file as FileStream).Name );
}
Run Code Online (Sandbox Code Playgroud)
和MyModel包含
public static Stream DownloadFiles(string file){
return new FileStream(file, FileMode.Open, FileAccess.Read);
}
Run Code Online (Sandbox Code Playgroud)
如果我using
在控制器中使用关键字,那么将抛出异常:Cannot access closed file
.
好吧,我想确保下载的文件将被处理(我不知道是否可能,如何做到)或不?
谢谢
Dmi*_*sev 11
Controller.File方法使用FileStreamResult类,里面已经包含using关键字
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8262 次 |
最近记录: |