0

I am trying to download PPT which exists in project folder to the download folder.

        System.IO.FileInfo file = new System.IO.FileInfo(HttpContext.Server.MapPath("~/Output/Document.pptx"));
        DownloadPPT("Document.pptx", file);  

This is DownloadPPT function :

    public void DownloadPPT(string fileName, System.IO.FileInfo file)
    {
        if (!file.Exists)
        {

        }
        else
        {
            // clear the current output content from the buffer
            Response.Clear();

            // add the header that specifies the default filename for the 
            // Download/SaveAs dialog 
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);

            //// add the header that specifies the file size, so that the browser
            //// can show the download progress
            //Response.AddHeader("Content-Length", file.Length.ToString());

            // specify that the response is a stream that cannot be read by the
            // client and must be downloaded
            Response.ContentType = "application/vnd.ms-powerpoint";
            // send the file stream to the client
            Response.WriteFile(Server.MapPath("~/Output/Document.pptx"));
        }
    }  

There is no error but ppt is not downloaded.
Can someone tell what is wrong in my code ?

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • 2
    if you are in an MVC Controller Action, you can use the MVC Utility methods like `return File` and have a return type of `FileResult`. – Dennis Jul 26 '13 at 07:05
  • Are you calling your first code snippet inside of a controller method? if so, could you post the code of the controller method (or at least from that code snippet to the end)? – Dennis Jul 26 '13 at 07:11
  • No I am not in ActionResult. I need to handle the download in this code itself. Thanks for the suggestion though. – Nitish Jul 26 '13 at 07:12
  • maybe file does not exist? : ) and code finishes execution at first if. Did you debug that? – archil Jul 26 '13 at 07:49
  • File exists. else part of condition is called. – Nitish Jul 26 '13 at 08:18

2 Answers2

1

You could try this piece of code:

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/vnd.ms-powerpoint";
Response.Buffer = true;

using (FileStream fileStream = File.Open(Server.MapPath("~/Output/Document.pptx"), FileMode.Open)
{
    fileStream.CopyTo(Response.OutputStream);
}

Response.End();

I cannot tell you exactly what could be wrong with your code, but I am using this snippet in quite a similar context and it has always worked for me. If it doesn't, maybe it is because of some other condition in your situation? It seems you have access to the Response property, that's why i cannot think of anything that would prevent this snippet from working correctly. But there sure are other guys with more experience than me if this does not help.

Dennis
  • 14,210
  • 2
  • 34
  • 54
  • Still not downloading :( – Nitish Jul 26 '13 at 07:30
  • 1
    I would recommend putting your code inside of an MVC action, if this is somehow possible. If not, could you provide more details in what kind of context you are? If this is some kind of background progress, you will not have the possibility to make the browser download a file, as the request has then probably already ended and the server cannot push new data to the browser anymore – Dennis Jul 26 '13 at 08:28
  • Your second reason might be correct. But I am not sure if its true and how to fix it. – Nitish Jul 26 '13 at 08:38
  • That would be a more complex situation. If your code is in some background process and the `Response` context is not available anymore, you have two basic options: either put your work in the request thread and hold the request open until you can send the document to the browser. this will not work though, if your main request is already sending some other information. Second option is to let the browser poll the status of the document via `javascript`. when the file is ready for download, you can answer that poll with a `url` which the browser can use to download the file (an MVC action). – Dennis Jul 26 '13 at 09:06
0

one suggestion is to use Iframe which will push the non-rendered format files to download..

More info here

Community
  • 1
  • 1
Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70