生产服务器上的文件下载问题
本文关键字:文件下载 问题 服务器 | 更新日期: 2023-09-27 18:14:42
我使用以下代码从服务器下载文件,该文件在每个浏览器的开发和QA服务器中工作良好,但当它进入生产时出现错误。错误是System.IO。目录notfoundexception路径部分不正确
使用代码:
protected void lnkDownload_Click(object sender, eventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
string filePath = ((HtmlInputHidden)gvFilesDetails.Rows[gvrow.RowIndex].FindControl("hdnFileLocation")).Value.ToString();
Response.AddHeader("Content-Disposition", "attachment;filename='"" + filePath + "'"");
Response.TransmitFile(Server.MapPath("~/" + filePath));
Response.End();
}
这个问题似乎是奇怪的和痛苦的调试,因为它只发生在生产服务器。
服务器。MapPath将获得与虚拟路径相对应的物理路径。请交叉检查您的应用程序服务器(生产)。
这是我在生产中使用的代码,对我来说很好,你可以看看它。
protected void imgDownload_click(object sender, ImageClickEventArgs e)
{
ImageButton imgbtn = (ImageButton)sender;
GridViewRow row = (GridViewRow)imgbtn.NamingContainer;
string strHFFileDetails =((HiddenField )row.FindControl("HFFileID")) .Value.ToString();
string[] strFileDetails = strHFFileDetails.Split('?');
string FilePath = strFileDetails[1].ToString();
string filename = gvFiles.Rows[row.RowIndex].Cells[0].Text.ToString();
string path = @FilePath;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AppendHeader("Content-Disposition:", "attachment; filename=" + file.Name);
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.TransmitFile(file.FullName);
Response.End();
}
}
@Tapas Mahata试试这个,
在web.config中定义你的web URL,
<appSettings>
<add key="FileURL" value="http:''www.WebSiteName.com'"/>
<appSettings>
在aspx.cs protected void lnkDownload_Click(object sender, eventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
string filePath = ((HtmlInputHidden)gvFilesDetails.Rows[gvrow.RowIndex].FindControl("hdnFileLocation")).Value.ToString();
string fileURL = ConfigurationManager.AppSettings["FileURL"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename='"" + filePath + "'"");
Response.TransmitFile(fileURL+filePath);
Response.End();
}
@Tapas Mahata试试这个,
aspx页面:
<asp:GridView ID="gv" OnRowDataBound="gv_RowDataBound" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkField Target="_blank" DataNavigateUrlFields="URL" DataTextField="URL" HeaderText="Document Path" />
</Columns>
</asp:GridView>
aspx.cs Page:
private string docPath = "";
protected void Page_Load(object sender, EventArgs e)
{
docPath = ResolveClientUrl("~/Uploads/");
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
HyperLink FileHyperlink = null;
if (e.Row.RowType == DataControlRowType.DataRow)
{
FileHyperlink = (HyperLink)e.Row.Cells[0].Controls[0];
string fileName = FileHyperlink.Text.Replace("'", "''");
FileHyperlink.NavigateUrl = docPath + fileName;
}
}