【问题标题】:File download issue on Production server生产服务器上的文件下载问题
【发布时间】:2013-05-17 06:46:17
【问题描述】:

我已使用以下代码从服务器下载文件,该服务器在每个浏览器的开发和 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();
}

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

【问题讨论】:

  • 如何在代码中添加一些日志记录以使其可以投入生产?
  • 您的 Web 应用程序是否有权写入生产服务器上的特定位置?
  • @chridam 是的,Web 应用程序已获得完全权限。
  • @watraplion: filepath 包含要下载的文件的完整路径,例如"附件\DownloadableFiles\ProjectOverview.pptx"
  • 你得到的值是多少 -> Server.MapPath("~/" + filePath) ?检查完整的 URL 是否正确。如果正确在浏览器中输入完整的 URL 并检查是否正在下载你的 pptx 文件??

标签: c# asp.net


【解决方案1】:

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

}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 对不起,同样的错误发生在生产上,但在开发和质量检查中工作正常。 :(
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多