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 ?

4

2 回答 2

1

你可以试试这段代码:

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();

我不能确切地告诉你你的代码有什么问题,但我在非常相似的上下文中使用这个片段,它一直对我有用。如果没有,也许是因为您的情况有其他情况?看来您可以访问该Response属性,这就是为什么我想不出任何会阻止此代码段正常工作的原因。但是如果这没有帮助,肯定还有其他人比我更有经验。

于 2013-07-26T07:22:51.067 回答
0

一个建议是使用 iframe,它将推动非渲染格式文件下载..

更多信息在这里

于 2013-07-26T07:40:40.697 回答