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

发布于 2024-07-16 02:18:17 字数 1788 浏览 8 评论 0原文

我有一个 Flex 应用程序,它在我们的网络服务器上调用 .aspx 页面,该页面会构建 PDF 或 Excel 文件。 现在,我正在使用 navigatorToURL() 来调用该文件,当输出文件成功构建时,这可以正常工作。 但是,如果出现错误或超时,则无法在 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);

I have a Flex application that calls a .aspx page on our webserver, which builds a PDF or Excel File. Right now, I am using navigateToURL() to call the file, and this works fine when the output file is built successfully. However, if there is an error, or timeout, there is no way of knowing this in the Flash movie.

I am trying to use URLLoader instead, so that I can listen for an HTTP Status Code, and know when the load is complete, however, the URLLoader simply returns the bitmap data, with nothing prompting the user to open or save the output file. Is there anyway to do this?

Here are both versions of my ActionScript code:

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");
}

FYI, here is the block of code that currently outputs the PDF or Excel file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-07-23 02:18:18

当用户请求该 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 中下载文件

You server should send "application/octet-stream" header when user requests that PSD file.

With ASP.NET it could look like this:

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

Also take a look here: Downloading files in Flex using the FileReference class

梦年海沫深 2024-07-23 02:18:18

还要在 aspx 页面中将 content-disposition 设置为“attachment”。

Also set the content-disposition to "attachment" in the aspx page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文