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

时间:2009-03-31 18:53:41

标签: asp.net flex actionscript-3 urlrequest urlloader

我有一个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);

2 个答案:

答案 0 :(得分: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();

另请看这里:Downloading files in Flex using the FileReference class

答案 1 :(得分:0)

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