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

2 回答 2

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 中下载文件

于 2009-03-31T18:55:38.597 回答
0

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

于 2009-03-31T18:56:54.367 回答