2

I have a page in my site which generates a PDF report dynamically on the server whilst displaying a "please wait" message to the users browser. After it has finished it puts the unique filepath in the session and then opens up download.aspx which is empty bar the following c# code in the page_load function.

    string fileUID = (string)(Session["fileUID"]);
        string FilePath = @fileUID;
        byte[] fileBytes = System.IO.File.ReadAllBytes(FilePath);
        string sFileName = "Report.pdf";

        System.Web.HttpContext context = System.Web.HttpContext.Current;
        context.Response.Clear();
        context.Response.ClearHeaders();
        context.Response.ClearContent();
        context.Response.AppendHeader("content-length", fileBytes.Length.ToString());
        context.Response.ContentType = "application/octet-stream";
        context.Response.AppendHeader("content-disposition", "attachment; filename=" + sFileName);
        context.Response.BinaryWrite(fileBytes);
        context.Response.Flush();
        System.IO.File.Delete(FilePath);
        context.ApplicationInstance.CompleteRequest();

In IE this brings up the download dialogue allowing the user to download the file. However in chrome, firefox and safari it just sits on the please wait page forever...

I have also tried specifying the mime type for a PDF file, and not specifying content-disposition to make the PDF display in the browser window, again works perfectly in internet explorer but not any other browser.

The issue occurs both when testing on localhost, and when uploaded to the server.

I have searched both on here and the wider world and cannot seem to find anybody else with the same issue.

Please can somebody show me the error of my ways.

4

3 回答 3

0

谢谢您的帮助。

问题出在请稍候对话页面上,我在其他浏览器中对其进行了调试,他们都讨厌 window.location(page),所以我将其更改为 window.open(page),它工作正常。

不知道我是怎么错过的。

于 2013-07-04T08:21:08.787 回答
0

您应该在创建文件时设置 cookie,如下所示:

 var cookie = new HttpCookie("fileDownloadToken", _token);
                    cookie.Expires = DateTime.Now.AddMinutes(10); 
                    cookie.Path = "/"; //Also set path
                    context.Response.AppendCookie(cookie);

如果将其设置为预期值,则带有 jquery.cookie 插件的下一页将读取 cookie 值,然后使用 jquery blockui 取消阻止 ui:

<script type="text/javascript">

    var fileDownloadCheckTimer;
    function blockUIForDownload() {
        var token = '1357.11.22'; 
        $('#download_token_value').val(token);
       $.blockUI({
            message:$('#domMessage'),
            css: {
                padding: 10,
                margin: 0,
                width: '30%',
                top: '50%',
                left: '35%',

                textAlign: 'center',
                color: '#000',
                border: '3px solid #aaa',
                backgroundColor: '#fff',
                cursor: 'wait',
            }});
            fileDownloadCheckTimer = window.setInterval(function () {
                var cookieValue = $.cookie('fileDownloadToken');
                //alert(cookieValue);
                if (cookieValue == token)
                    finishDownload();
            }, 1000);
    }

    function finishDownload() {
        window.clearInterval(fileDownloadCheckTimer);
        $.unblockUI();
        $.cookie('fileDownloadToken', null, { path: '/' });

    }

</script>  
于 2013-07-04T08:50:12.807 回答
0

我就是这样

    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";//check your valid file type
    Response.AddHeader("Content-Disposition", "attachment;filename=" + targetFileName);
    Response.Charset = "";

    using (System.IO.StreamWriter writer = new StreamWriter(Response.OutputStream)
    {
        //writer.Write(contentBytes);
    }
    Response.Flush();
    Response.Close();
    Context.ApplicationInstance.CompleteRequest();

确保在调用 CompletedRequest() 之前关闭或处置所有流。

于 2013-07-04T08:17:18.660 回答