最近在搞毕业项目。做到有关文件的上传下载方面的问题。

Code
protected void gvAccessoryFileInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';cursor='hand';";
e.Row.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#dedfde';";
int accessoryId = Convert.ToInt32(gvAccessoryFileInfo.DataKeys[e.Row.RowIndex].Value.ToString());
IList<AccessoryFile> acceList = AccessoryFileManager.GetAllEntities().Where(c => c.Id == accessoryId).ToList();
AccessoryFile acceFile = acceList[0];
string path = acceFile.AccessoryPath;
string mappath = Server.MapPath(path);
HyperLink hylinkAccessory = (HyperLink)e.Row.FindControl("hylinkAccessory");
hylinkAccessory.ToolTip = "点击下载";
hylinkAccessory.Text = acceFile.AccessoryName;
hylinkAccessory.NavigateUrl = string.Format("~/DownHandler.ashx?id={0}",acceFile.Id);
}
是在GridView里面设置了个hylink连接。把具体下载的请求,ID发到handler处理

protected void gvAccessoryFileInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';cursor='hand';";
e.Row.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#dedfde';";
int accessoryId = Convert.ToInt32(gvAccessoryFileInfo.DataKeys[e.Row.RowIndex].Value.ToString());
IList<AccessoryFile> acceList = AccessoryFileManager.GetAllEntities().Where(c => c.Id == accessoryId).ToList();
AccessoryFile acceFile = acceList[0];
string path = acceFile.AccessoryPath;
string mappath = Server.MapPath(path);
HyperLink hylinkAccessory = (HyperLink)e.Row.FindControl("hylinkAccessory");
hylinkAccessory.ToolTip = "点击下载";
hylinkAccessory.Text = acceFile.AccessoryName;
hylinkAccessory.NavigateUrl = string.Format("~/DownHandler.ashx?id={0}",acceFile.Id);
}


public void ProcessRequest (HttpContext context) {
int id=Convert.ToInt32(context.Request.Params["id"].ToString());
if (id > 0) {
AccessoryFile acceFile = AccessoryFileManager.FetchEntityByKey(id);
string fileName = acceFile.AccessoryName.ToString();
//下面的返回包含指定网站的虚拟目录的物理路径 因为是隐含区,所以要加HttpContext.Current才可以点出Server
string filePath = HttpContext.Current.Server.MapPath(acceFile.AccessoryPath + "\\" + fileName);
if (System.IO.File.Exists(filePath))
{
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
context.Response.ContentType = "application/octet-stream";
// 通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.End();
}
else
{
context.Response.ContentType = "text/html";
context.Response.Write("<script>alert('文件不存在或者已被删除')</script>");
context.Response.Write("<a href='/Web/Pages/DocumentMag/FileSearch.aspx'>返回查询页</a>");
context.Response.BufferOutput = true;
}
}