如何提示用户打开或保存 .aspx 文件返回的 PDF 文件?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当用户请求该 PSD 文件时,您的服务器应该发送“application/octet-stream”标头。
对于 ASP.NET,它可能如下所示:
另请查看此处: 使用 FileReference 类在 Flex 中下载文件
You server should send "application/octet-stream" header when user requests that PSD file.
With ASP.NET it could look like this:
Also take a look here: Downloading files in Flex using the FileReference class
还要在 aspx 页面中将 content-disposition 设置为“attachment”。
Also set the content-disposition to "attachment" in the aspx page.