tags:

views:

33

answers:

3

Hello All,

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.

A: 

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.

adamcodes
I'm not actually uploading any files, though. Just posting data from TextInput.
taudep
Here's a link to the Adobe docs, It seems I should be able to do what I want, without any call to fileRef.upload, since I'm not actually uploading a file: http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_7.html (Look at the section Downloading files from a server)
taudep
You would then need to use URLRequest to hold the name of the file. Also the URLRequest would be the only parameter to download, since you're not changing the name. HTH
adamcodes
A: 

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.

Ross Henderson
A: 

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:

private var fileRef:FileReference;
private function saveRawHierarchy(event:MouseEvent):void
{                                   
    var fileRequest:URLRequest = new URLRequest();
    fileRequest.method = URLRequestMethod.POST;
    fileRequest.url = "http://foo/bounceback";

    var urlVariables:URLVariables = new URLVariables();             
    urlVariables.content = "CONTENT HERE";
    urlVariables.fileName = "newFileName.xml";

    fileRequest.data = urlVariables;

    fileRef = new FileReference();                   
    fileRef.addEventListener(Event.COMPLETE, onComplete); 

    try
    {
        fileRef.download(fileRequest, "appHierarchies.xml");
    }catch(error:Error) {
        model.logger.error("unable to download file");
    }       
}

Not sure what was wrong about the request not being made before, though.

taudep