0

I am allowing users to download either a PDF file or a zip file, and when they try to download the file, I want the appropriate file to be downloaded according to its type. For example: if the uploaded file is PDF, then it should be downloaded as a PDF; if the uploaded file is zip, then it should downloaded as a zip file.

I have written this code and I am able to download the files as PDF using "output.pdf" in the append header, but don't know how to give two options to append header so that it downloads the file according to its type.

 protected void gridExpenditures_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            Response.Clear();
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("content-disposition", "FileName=" + e.CommandArgument + "output.pdf");
            Response.TransmitFile(Server.MapPath("~/Match/Files/") + e.CommandArgument);
            Response.End();
        }
    }
famousgarkin
  • 13,687
  • 5
  • 58
  • 74

2 Answers2

1

You can use a utility like this one to detect the content type of the file in question, then render the header like this:

protected void gridExpenditures_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
        var filePath = Server.MapPath("~/Match/Files/") + e.CommandArgument;
        var contentType = MimeTypes.GetContentType(filePath);
        if (string.IsNullOrEmpty(contentType))
        {
            contentType = "application/octet-stream";
        }
        Response.Clear();
        Response.ContentType = contentType;
        Response.AppendHeader("content-disposition", "FileName=" + e.CommandArgument);
        Response.TransmitFile(filePath);
        Response.End();
    }
}
Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

You need to set your content type to the appropriate application, instead of octet-stream.

For example I had this to open PowerPoint:

application/vnd.openxmlformats-officedocument.presentationml.presentation

Look up your file type in this link: http://en.wikipedia.org/wiki/Internet_media_type

I store the uploaded content type in my database for each file.

  • So how do we set our content type for two types of application simultaneously? Is there a way? – user3727052 Jun 24 '14 at 18:21
  • A file should really only be associated with a single content type. A zip may contain a PDF... But it is still a ZIP, which will be handled according to the settings on the OS. What would be the use case on when you need two types? – RadioActiveEd Jun 24 '14 at 18:30
  • Most of the time users will send me only one file as pdf but there will be cases in which they will send me multiple files in a zip file which will have all kinds of files like jpeg,doc,xml etc so thats why I trying to give a option of downloading files according to the file type. – user3727052 Jun 24 '14 at 18:36
  • If you are storing the ZIP file, then they will be downloading a ZIP file. They will need to extract the individual files themselves. The application, nor you, nor the OS, knows what's in the ZIP file. If you tried to associate the ZIP file with PowerPoint, when PowerPoint opens, it will throw an error at the user. – RadioActiveEd Jun 24 '14 at 18:40
  • No I am not trying to do that I know its not possible what I am trying to do is when they click on a pdf file in gridview the file is downloaded as pdf and when they click on zip file it is downloaded as zip file. I am using GUIDs to name file therefore right now all files are downloaded without any type because their extension has a GUID attached to them. – user3727052 Jun 24 '14 at 18:44
  • When you upload the file, you need to store the information somewhere. When they click on a certain link in your gridview, it will need to recall this information. If they click on ZIP, and you want to ZIP the PDF, that's another story. Regardless, each link inside your gridview should be unique. What you do to that link to determine the application type is up to you. Maybe the ZIP is in column A, and PDF in B... doesn't matter. You will need some way of knowing what they clicked on. – RadioActiveEd Jun 24 '14 at 18:55