The gridview is binding the images directly from the folder whose location is in the solution itself.
The folder name is Images
The gridview is successfully binding but when clicking the download button i fails
private void BindGrid()
{
try
{
string[] ImagePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
if (ImagePaths != null)
{
if (ImagePaths.Length > 0)
{
AddScreenShots(ImagePaths);
}
else
grdRadar.EmptyDataText = "No images available for current month";
}
grdRadar.EmptyDataText = "No images available for current month";
}
catch (Exception ex)
{
grdRadar.EmptyDataText = "" + "Directory Not found";
}
finally
{
grdRadar.DataBind();
}
}
private void AddScreenShots(string[] pathArr)
{
DataTable dt = new DataTable();
dt.Columns.Add("ImageID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Path", typeof(string));
int count = 0;
foreach (string imgPath in pathArr)
{
string ImgName = Path.GetFileName(imgPath);
dt.Rows.Add(count++, ImgName, imgPath);
}
grdRadar.DataSource = dt;
grdRadar.DataBind();
}
protected void lnkDownload_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
string filePath = grdRadar.DataKeys[gvrow.RowIndex].Value.ToString();
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}
THE error is coming in line
Response.TransmitFile(Server.MapPath(filePath));
System.Web.HttpException was unhandled by user code HResult=-2147467259 Message='E:/tEST//TestWeb/iMAGES/IMG1.png' is a physical path, but a virtual path was expected. Source=System.Web ErrorCode=-2147467259 WebEventCode=0 StackTrace: at System.Web.Util.UrlPath.CheckValidVirtualPath(String path) at System.Web.Util.UrlPath.Combine(String appPath, String basepath, String relative) at System.Web.Util.UrlPath.Combine(String basepath, String relative) at System.Web.VirtualPath.Combine(VirtualPath relativePath) at System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping) at System.Web.HttpRequest.MapPath(VirtualPath virtualPath) at System.Web.HttpRequest.MapPath(String virtualPath) at System.Web.HttpServerUtility.MapPath(String path) at TestWeb.Radar.lnkDownload_Click(Object sender, EventArgs e) in e:\info-banjir\lastest\InfobanjirV2.JPS\TestWeb\Radar.aspx.cs:line 76 at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException:
<asp:GridView runat="server" EmptyDataText="No Records Found !" DataKeyNames="Path"
AllowPaging="false" OnPageIndexChanging="OnPaging" PagerSettings-Mode="NumericFirstLast"
Width="100%" CssClass="Grid" HeaderStyle-CssClass="GridHeader" AlternatingRowStyle-CssClass="GridAtlItem"
AlternatingRowStyle-Wrap="true" RowStyle-Wrap="true" ID="grdRadar">
<Columns>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="lnkView_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can i fix this ???