0

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

Attachment is a link button to download file

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.

smn.tino
  • 2,272
  • 4
  • 32
  • 41
  • There are several ways to download files from websites. Did you search stackoverflow for examples? One quick search gave me this: https://stackoverflow.com/questions/525364/how-to-download-a-file-from-a-website-in-c-sharp – Christian Jul 28 '18 at 17:22
  • Why not store the filename with extension as a CommandArgument? – VDWWD Jul 28 '18 at 17:53
  • @Christian, mate i suppose u didnt read my question properly, i asked for the way to download files with different extentions. – Ashish Sharma Jul 29 '18 at 10:52
  • Whatever extension you want to download is irrelevant at this point - you supply the full path and the browser takes care of the rest. If you don't know the extension, you should lookup that first. – Christian Aug 01 '18 at 10:23

0 Answers0