0

我已经使用以下代码从服务器下载文件,该服务器在每个浏览器的开发和 QA 服务器中都可以正常工作,但是当它投入生产时出现错误。错误是System.IO.DirectoryNotFoundException 部分路径不正确

使用的代码:

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();
}

这个问题似乎很奇怪而且调试起来很痛苦,因为它只发生在生产服务器中。请帮帮我。

4

3 回答 3

0

@Tapas Mahata 试试这个,

在 web.config 中定义您的网址,

<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();
}
于 2013-05-22T10:44:11.840 回答
0

@Tapas Mahata 也试试这个,

页面:

<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 页面:

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;
    }
}
于 2013-05-22T11:04:25.730 回答
0

Server.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();
    }

}
于 2013-05-22T09:34:26.420 回答