sp.net web api写的,服务器环境IIS7
我不想任何人都可以下载,所以需要验证,通过了才可以下载(仅下载需要验证,其他api不要验证),所以我做了一个download控制器,具体代码如下,然后我对这个stream的释放不是很了解,我以前写WPF的,用了的流都要释放,现在这个地方这个流我怎么来释放?本身返回的就是流,客户端要根据这个流来下载文件,我肯定不可能释放了吧?如果我不释放不会有问题?流、数据库连接这种不是必须要手动释放的么?请解惑
public HttpResponseMessage Get(string id)
{
string path = @"E:\1.mpg";
HttpResponseMessage respone;
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
respone = new HttpResponseMessage { Content = new StreamContent(fs) };
respone.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
respone.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("file") { FileName = "1.mpg" };
return respone;
}
catch (FileNotFoundException)
{
return new HttpResponseMessage(HttpStatusCode.NotFound); //找不到资源
}
catch (ArgumentException)
{
return new HttpResponseMessage(HttpStatusCode.NotFound); //找不到资源
}
我不想任何人都可以下载,所以需要验证,通过了才可以下载(仅下载需要验证,其他api不要验证),所以我做了一个download控制器,具体代码如下,然后我对这个stream的释放不是很了解,我以前写WPF的,用了的流都要释放,现在这个地方这个流我怎么来释放?本身返回的就是流,客户端要根据这个流来下载文件,我肯定不可能释放了吧?如果我不释放不会有问题?流、数据库连接这种不是必须要手动释放的么?请解惑
public HttpResponseMessage Get(string id)
{
string path = @"E:\1.mpg";
HttpResponseMessage respone;
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
respone = new HttpResponseMessage { Content = new StreamContent(fs) };
respone.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
respone.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("file") { FileName = "1.mpg" };
return respone;
}
catch (FileNotFoundException)
{
return new HttpResponseMessage(HttpStatusCode.NotFound); //找不到资源
}
catch (ArgumentException)
{
return new HttpResponseMessage(HttpStatusCode.NotFound); //找不到资源
}