文件下载

次下载方法是从数据库读取出文件流进行下载的

/// <summary>
/// 下载文件
/// </summary>
/// <param name="context"></param>
public virtual void DownloadFiles()
{
try
{
string fileId = context.Request.QueryString["FileId"];
// fileId = "d084cf5ac0c44aa380558a7232da5385";
if (fileId != null && fileId.Length > 0)
{
IFileService fileComponent = ServiceContainer.GetService<IFileService>();
///获取文件头信息
MetaFileEntity metaFileEntity = fileComponent.GetMetaFile(fileId);
if (metaFileEntity == null)
throw new Exception();
///获取文件的数据
byte[] data = fileComponent.GetFileData(fileId, metaFileEntity.Suffixal);

string contextType = "application/octet-stream";
HttpContext.Current.Response.ContentType = contextType;
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + context.Server.UrlEncode(metaFileEntity.Name));
HttpContext.Current.Response.AddHeader("Content-Length", data.Length.ToString());
context.Response.BinaryWrite(data);
}
}
catch (Exception ex)
{
Logger.Log.Error(this.GetType() + ", 上传出错------------------" + ex);
context.Response.Redirect("/web/Page/Download/FileNotFound.html");
}
ResponseEnd();
}

posted @ 2012-03-19 11:42  JasonRen  阅读(137)  评论(0)    收藏  举报