I have a nested scenario with gridview and there is a requirement of downloading a file from the child gridview.
I am fetching the path but not getting an appropriate way to download the file as it has different types, like .xls,.docx,.png,.jpg etc.
here is a snip of Grid
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "AttachDownload")
{
WebClient webClient = new WebClient();
string url = e.CommandArgument.ToString();
byte[] bytes = webClient.DownloadData(url);
string fileName = e.CommandSource.ToString();
// Response.ContentType = "image/png";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.End();
// ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Download Attachment')", true);
}
}
catch (Exception ex) { lblErrorText.Text = ex.ToString(); }
}
The Row command event is fired and command argument is the file's name.