当前位置:  开发笔记 > 后端 > 正文

如何提示用户打开或保存.aspx文件返回的PDF文件?

如何解决《如何提示用户打开或保存.aspx文件返回的PDF文件?》经验,为你挑选了1个好方法。

我有一个Flex应用程序,它在我们的网络服务器上调用.aspx页面,该页面构建一个PDF或Excel文件.现在,我使用navigateToURL()来调用文件,这在成功构建输出文件时工作正常.但是,如果出现错误或超时,则无法在Flash影片中知道这一点.

我正在尝试使用URLLoader,以便我可以侦听HTTP状态代码,并知道何时加载完成,但是,URLLoader只返回位图数据,没有提示用户打开或保存输出文件.反正有没有这样做?

以下是我的ActionScript代码的两个版本:

private function buildFile():void
{
    var variables:URLVariables = new URLVariables();

    variables.rptName = "report1";

    variables.rptFormat = "PDF";

    var request:URLRequest = new URLRequest();

    request.url = "/buildFile.aspx";

    request.method = URLRequestMethod.POST;

    request.data = variables;

    var loader:URLLoader = new URLLoader();

    loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, 
                                        httpStatusHandler);

    loader.load(request);
}


private function buildFile():void
{
    var variables:URLVariables = new URLVariables();

    variables.rptName = "report1";

    variables.rptFormat = "PDF";

    var request:URLRequest = new URLRequest();

    request.url = "/buildFile.aspx";

    request.method = URLRequestMethod.POST;

    request.data = variables;

    navigateToURL(request, "_self");
}

仅供参考,这是当前输出PDF或Excel文件的代码块:

System.Web.HttpContext httpC;
httpC = System.Web.HttpContext.Current;
httpC.Response.Clear();
httpC.Response.ClearHeaders();
httpC.Response.Expires = 0;
switch (ReportFormat)
{
    case "Excel": Response.ContentType = "application/vnd.ms-excel";  break;

    case "PDF": Response.ContentType = "application/pdf"; ; break;

}

Response.AddHeader("Content-disposition", "attachment;filename=" + fileName);    

BinaryWriter binaryWriter = new BinaryWriter(httpC.Response.OutputStream);

binaryWriter.Write(result);

Konstantin T.. 6

当用户请求PSD文件时,您的服务器应发送"application/octet-stream"标头.

使用ASP.NET,它可能如下所示:

Response.ClearContent();
Response.ClearHeaders();
// with this header user will be promted to open or download file
Response.ContentType = "application/octet-stream";
// with this header, you will suggest to save file with "test.pdf" name
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.WriteFile(Server.MapPath("~/test.pdf"));
Response.Flush();
Response.Close();

另请参阅此处:使用FileReference类在Flex中下载文件



1> Konstantin T..:

当用户请求PSD文件时,您的服务器应发送"application/octet-stream"标头.

使用ASP.NET,它可能如下所示:

Response.ClearContent();
Response.ClearHeaders();
// with this header user will be promted to open or download file
Response.ContentType = "application/octet-stream";
// with this header, you will suggest to save file with "test.pdf" name
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.WriteFile(Server.MapPath("~/test.pdf"));
Response.Flush();
Response.Close();

另请参阅此处:使用FileReference类在Flex中下载文件

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有