从 Flex 下载作为文件发布到服务器的数据
这应该是微不足道的,而且我很确定我以前做过一次。
我正在尝试将数据发布到服务器,并将其作为文件下载反弹给我,从而提示本机浏览器文件下载框。我知道服务器部分工作得很好,因为我可以从演示 Web 表单发布,但是当我运行以下 Flex 3 代码时,我什至无法触发请求。
var fileRef:FileReference = new FileReference();
private function saveXmlAsFile(event:MouseEvent):void
{
var fileRequest:URLRequest = new URLRequest();
fileRequest.method = URLRequestMethod.POST;
fileRequest.url = "http://foo.com/dataBounce";
var urlVariables:URLVariables = new URLVariables();
urlVariables.content = "Test content to return" ;
// fileRequest.contentType = "application/x-www-form-urlencoded ";
urlVariables.fileName = "test.xml";
fileRef.addEventListener(SecurityEvent.ALL, onSecurityError);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError2);
fileRef.addEventListener(IOErrorEvent.NETWORK_ERROR, onNetworkError);
fileRef.addEventListener(Event.COMPLETE, onComplete);
try
{
fileRef.download(fileRequest, "test.xml");
}catch(error:Error) {
model.logger.error("unable to download file");
}
}
请注意,当调用 fileRef.download 时,我看不到使用传统 Firebug 或 HTTPWatch 浏览器工具通过网络发出的任何请求。
编辑:我应该补充一点,这是针对 < Flash Player 10,因此我无法使用较新的直接另存为文件功能。
有什么建议吗?谢谢。
This should be trivial, and I'm pretty sure I did it once before.
I'm trying to post data up to a server and have it bounced back to me as a file download, prompting the native browser file download box. I know the server part works just fine becasue I can post from a demo web form, but when I run the following Flex 3 code, I can't even get the request to fire.
var fileRef:FileReference = new FileReference();
private function saveXmlAsFile(event:MouseEvent):void
{
var fileRequest:URLRequest = new URLRequest();
fileRequest.method = URLRequestMethod.POST;
fileRequest.url = "http://foo.com/dataBounce";
var urlVariables:URLVariables = new URLVariables();
urlVariables.content = "Test content to return" ;
// fileRequest.contentType = "application/x-www-form-urlencoded ";
urlVariables.fileName = "test.xml";
fileRef.addEventListener(SecurityEvent.ALL, onSecurityError);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError2);
fileRef.addEventListener(IOErrorEvent.NETWORK_ERROR, onNetworkError);
fileRef.addEventListener(Event.COMPLETE, onComplete);
try
{
fileRef.download(fileRequest, "test.xml");
}catch(error:Error) {
model.logger.error("unable to download file");
}
}
Note, when the call to fileRef.download is called, I can't see any request being made across the network using the traditional Firebug or HTTPWatch browser tools.
EDIT: I should add that this is for < Flash Player 10, so I can't use the newer direct save as file functionality.
Any suggestions? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要添加
fileRef.upload
来触发上传。另外,我会将下载语句移至
onComplete
,以便在上传文件之前不会请求该文件。You need to add
fileRef.upload
to trigger the upload.Also I would move the download statement to the
onComplete
so the file isn't requested before it's been uploaded.你的解释很清楚,但是当我看你的代码时,我觉得我错过了一些东西。
该代码看起来像是您正在尝试执行一半的上传部分和一半的下载部分。
我认为如果您将
.method
值设置为 GET,您当前发布的代码将可以触发下载。我相信您还需要将文件名作为.url
属性的一部分包含在内。但是,要发布某些内容然后触发其下载,您需要两个单独的操作 - 发布数据的操作和下载数据的操作,这可能应该从上传操作的 onComplete 处理程序中调用。
Your explanation is pretty clear, but when I look at your code, I'm feel like I'm missing something.
The code looks like you're trying to do half of the upload part and half of the download part.
I think the code you currently have posted would work to trigger a download if you set the
.method
value to GET. I believe you will also need to include the filename as part of the.url
property.However, to post something and then trigger a download of it, you need two separate operations - the operation to post the data and then an operation to download it, which should probably be called from the upload operation's onComplete handler.
好吧,我相信我已经弄清楚了正在发生的事情之一。
当您未设置 URLRequest.data 属性时,它将默认请求方法为“GET”。
因此,工作代码看起来像这样,将数据设置为发布的 URL 变量:
不过,不确定之前未发出的请求出了什么问题。
OK, I believe I figured out one of the things that's going on.
When you don't set the URLRequest.data property, it defaults the request method to "GET".
So, the working code looks like, with the data set to the posted URL variables:
Not sure what was wrong about the request not being made before, though.