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これも試してみてください。

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;
    }
}
于 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 に答える
0

@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();
}
于 2013-05-22T10:44:11.840 に答える