生产服务器上的文件下载问题

时间:2013-05-22 09:17:37

标签: c# asp.net

我使用以下代码从服务器下载文件,这些文件在开发和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();
}

调试问题似乎很奇怪,因为它只发生在生产服务器中。 请帮帮我。

3 个答案:

答案 0 :(得分: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();
    }

}

答案 1 :(得分: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();
}

答案 2 :(得分:0)

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

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