GridView 中下载文件
GridView 中每一行都有一个File Name 列,显示文件名称,点击后可下载.
前台GridView :
<asp:TemplateField HeaderText="File Name" SortExpression="FULLFILENAME">
<itemtemplate>
<asp:LinkButton id="lbnFileName" onclick="lbnFileName_Click" runat="server" Text='<%# Eval("FULLFILENAME") %>'></asp:LinkButton>
<asp:Label id="lblGUIDFILENAME" runat="server" Text='<%# Eval("GUID_FILENAME") %>' Visible="False"></asp:Label>
<asp:Label id="lblTYPENAME" runat="server" Text='<%# Eval("TYPENAME") %>' Visible="False"></asp:Label>
</itemtemplate>
</asp:TemplateField>
后台cs:
代码
protected void lbnFileName_Click(object sender, EventArgs e)
{
try
{
GridViewRow objGridViewRow = (GridViewRow)((LinkButton)sender).NamingContainer;
string strDQAManualFolder = Server.MapPath(@"~\DOC\Manual");
string strFileName = ((LinkButton)objGridViewRow.FindControl("lbnFileName")).Text;
string strGuid = ((Label)objGridViewRow.FindControl("lblGUIDFILENAME")).Text;
string strFileType = ((Label)objGridViewRow.FindControl("lblTYPENAME")).Text;
String fileName = HttpUtility.UrlEncode(strFileName);
FileInfo file = new FileInfo(strDQAManualFolder + "\\" + strGuid + '.' + strFileType);
if (file.Exists == false)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "FileNotFound", "alert('System can not find the file!');", true);
return;
}
switch (strFileType)
{
case "doc":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/octet-stream";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "ppt":
case "pps":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/vnd.ms-powerpoint";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "xls":
case "csv":
case "xml":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/vnd.ms-excel";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "pdf":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/pdf";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "htm":
case "html":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "text/html";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "gif":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "image/gif";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "jpg":
case "jpeg":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "image/jpeg";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "rtf":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/rtf";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "asp":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "text/asp";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
default:
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/octet-stream";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
}
}
catch (Exception ex)
{
this.ShowMessage("Download function exception!");
Response.Write(ex.ToString());
}
}
{
try
{
GridViewRow objGridViewRow = (GridViewRow)((LinkButton)sender).NamingContainer;
string strDQAManualFolder = Server.MapPath(@"~\DOC\Manual");
string strFileName = ((LinkButton)objGridViewRow.FindControl("lbnFileName")).Text;
string strGuid = ((Label)objGridViewRow.FindControl("lblGUIDFILENAME")).Text;
string strFileType = ((Label)objGridViewRow.FindControl("lblTYPENAME")).Text;
String fileName = HttpUtility.UrlEncode(strFileName);
FileInfo file = new FileInfo(strDQAManualFolder + "\\" + strGuid + '.' + strFileType);
if (file.Exists == false)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "FileNotFound", "alert('System can not find the file!');", true);
return;
}
switch (strFileType)
{
case "doc":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/octet-stream";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "ppt":
case "pps":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/vnd.ms-powerpoint";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "xls":
case "csv":
case "xml":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/vnd.ms-excel";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "pdf":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/pdf";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "htm":
case "html":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "text/html";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "gif":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "image/gif";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "jpg":
case "jpeg":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "image/jpeg";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "rtf":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/rtf";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
case "asp":
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "text/asp";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
default:
Response.Clear();
//Response.HeaderEncoding = System.Text.;
//欲讓使用者看到的檔案名稱
Response.AddHeader("Content-Disposition", "filename=" + fileName);
//檔案大小
Response.AddHeader("Content-Length", file.Length.ToString());
//檔案類型
Response.ContentType = "application/octet-stream";
//顯示檔案下載視窗
Response.TransmitFile(file.FullName);
Response.End();
break;
}
}
catch (Exception ex)
{
this.ShowMessage("Download function exception!");
Response.Write(ex.ToString());
}
}