通过数据库中的给定路径使用ASP.NET下载文件。它给我一个错误'C:/ Users / .....'是一个物理路径,但是预计会有虚拟路径。 [英] download file using ASP.NET by given path in database. Its Giving me an error 'C:/Users/.....' is a physical path, but a virtual path was expected.
本文介绍了通过数据库中的给定路径使用ASP.NET下载文件。它给我一个错误'C:/ Users / .....'是一个物理路径,但是预计会有虚拟路径。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void GridViewContact_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName ==Download)
{
GridViewRow gvr =(GridViewRow)((Control)e.CommandSource).NamingContainer;
int rowIndex = gvr.RowIndex;
GridViewRow row =(GridViewRow)GridViewContact.Rows [rowIndex];
Label resumename =(Label)row.FindControl(lblResumeName);
cn.Open();
SqlCommand com = new SqlCommand(select * from EmailLogs ResumeName ='+ resumename.Text +',cn);
SqlDataReader dr = com.ExecuteReader();
if(dr.Read())
{
HttpResponse Response = HttpContext.Current.Response;
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType =application / xlsx;
Response.AddHeader(content-disposition,attachment; filename =+ dr [ResumeName]。ToString()); //打开文件提示框打开或保存文件
Response.Charset =;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
// ResumePath是列名
//错误'C:/ Users / .....'是物理路径,但是需要虚拟路径。
Response.WriteFile(Server.MapPath(dr [ResumePath]。ToString()));
Response.End();
}
}
}
解决方案
如注释中所述,Server.MapPath采用虚拟路径(类似〜/ Pages / Documents / Document.aspx或/Pages/Document.aspx)。你正在通过物理路径。
因为你有物理路径删除Server.MapPath或者它真的不是一个真正的物理路径然后解析它以便Server.MapPath可以工作。 / BLOCKQUOTE>
protected void GridViewContact_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Download") { GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer; int rowIndex = gvr.RowIndex; GridViewRow row = (GridViewRow)GridViewContact.Rows[rowIndex]; Label resumename = (Label)row.FindControl("lblResumeName"); cn.Open(); SqlCommand com = new SqlCommand("select * from EmailLogs where ResumeName ='" + resumename.Text + "'", cn); SqlDataReader dr = com.ExecuteReader(); if (dr.Read()) { HttpResponse Response = HttpContext.Current.Response; Response.Clear(); Response.Buffer = true; Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/xlsx"; Response.AddHeader("content-disposition", "attachment;filename=" + dr["ResumeName"].ToString()); // to open file prompt Box open or Save file Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); // ResumePath is column name // Error 'C:/Users/.....' is a physical path, but a virtual path was expected. Response.WriteFile(Server.MapPath(dr["ResumePath"].ToString())); Response.End(); } } }解决方案As mentioned in comments, Server.MapPath takes a virtual path (something like ~/Pages/Documents/Document.aspx or /Pages/Document.aspx). You are passing in a physical path.
Since you have the physical path remove the Server.MapPath or if it really isn't a true physical path then parse it so that Server.MapPath will work.
这篇关于通过数据库中的给定路径使用ASP.NET下载文件。它给我一个错误'C:/ Users / .....'是一个物理路径,但是预计会有虚拟路径。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文