Dynamic Flash

Confessions of a serial code abuser
  • rss
  • Home
  • MTASC
  • Archives
  • About me
  • Goodies
    • Base64 encoder/decoder class
  • My Bookshelf
  • My Talks

FileReference.upload doesn’t support custom headers

Monday, 14 January 2008

At work we're working on an application that makes heavy use of the FileReference.upload() method to upload files to the server. We recently had a change request to add some custom HTTP headers to the upload request, which I didn't think would be a problem. I was wrong.

When using the FileReference.upload() method to upload a file to a server, you can specify additional variables to be sent along with that upload request by means of a URLRequest object like so:

PLAIN TEXT
Actionscript:
  1. // Create new request object and point it at the right URL
  2. var request:URLRequest = new URLRequest();
  3. request.url = "/upload.php";
  4.  
  5. // Specify additional variables to be sent with request as POST data
  6. request.method = URLRequestMethod.POST;
  7. request.data = new URLVariables();
  8. request.data.foo = "bar";
  9.  
  10. // Upload file using data from request object
  11. fileRef.upload(request);

Since you can send custom headers using the URLRequest object's requestHeaders property, I should have been able to specify custom headers for my file upload request fairly easily:

PLAIN TEXT
Actionscript:
  1. // Create new request object and point it at the right URL
  2. var request:URLRequest = new URLRequest();
  3. request.url = "/upload.php";
  4.  
  5. // Specify additional variables to be sent with request as POST data
  6. request.method = URLRequestMethod.POST;
  7. request.data = new URLVariables();
  8. request.data.foo = "bar";
  9.  
  10. // Specify additional headers to be sent with request
  11. var header:URLRequestHeader = new URLRequestHeader('X-Test-Header', 'Hello');
  12. request.requestHeaders.push(header);
  13.  
  14. // Upload file using data from request object
  15. fileRef.upload(request);

Sadly, the above code doesn't work. I checked the raw request and response using Charles and the custom X-Test-Header I was trying to send just wasn't coming through. I tried a number of different variations of the same code, all with the same result.

The problem, I have just discovered, is that there are a number of restrictions placed on the URLRequest object when used in conjunction with the FileReference methods. One of these restrictions is that the requestHeaders property is completely ignored when using the FileReference.upload() method, so it's impossible to send custom HTTP headers along with a file upload.

To be completely fair to Adobe, this and the other restrictions are stated quite clearly in the documentation for the URLRequest object:

The FileReference.upload() and FileReference.download() methods do not support the URLRequest.requestHeaders parameter.

Since I can't see any security or technical reason why the URLRequest object should be restricted in this way, I'm off to file a bug with Adobe to see if we can get this addressed in a future version of the Flash Player.

Lesson learned: RTFM.

Categories
ActionScript, Flash, Flex 2, Flex 3
Tags
ActionScript, air, file upload, filereference, Flash, flex, urlrequest
Comments rss
Comments rss
Trackback
Trackback

« Foundation ActionScript 3.0 now available foundationas3.com has launched »

4 responses

flash player 9 support it in swfupload you can

jiayin | Friday, 29 February 2008 | 10:37 am

flash player 9 support it
in swfupload
you can see the as source of swfupload flash9.0 version

jiayin: The above code is ActionScript 3.0 targeting Flash Player

Steve | Friday, 29 February 2008 | 11:06 am

jiayin: The above code is ActionScript 3.0 targeting Flash Player 9. According to the latest URLRequest.requestHeaders documentation still doesn’t work with FileReference.upload.

I’ve had a look at the swfupload source code and I can’t see anything in there that adds custom headers to a file upload.request.

You are right. This is a real problems. The Filereference.upload doesn't

icao | Friday, 06 June 2008 | 11:47 am

You are right.
This is a real problems. The Filereference.upload doesn’t send any Header to the server if you don’t use IE with windows.
All the information here:

https://bugs.adobe.com/jira/browse/FP-78

While this is a little late to be useful, I

Jackson Gabbard | Friday, 03 October 2008 | 2:27 am

While this is a little late to be useful, I figured for the web searchers like myself encountering this issue for te first time as well, that custom headers aren’t the only way to get additional info to the server along with binary data.

Cookies are always sent along with requests, so if you setup an ExternalInterface method to update a cookie via JS, you can read the cookie data on the server side.

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About Dynamic Flash

Steve Webster is a Senior Web Developer for Yahoo! in London, UK.

He is more than a little concerned that he defines himself in terms of his career, and that he talks about himself in the third person.

Find out more

Recent Posts

  • FotB ‘08 Schedule: iCal feeds
  • Read and write local files with Flash Player 10
  • The problem with SproutCore
  • Yahoo! London seeks Junior Developers
  • I am Singularity

Tags

Accessibility ActionScript actionscript3 actionscript 3.0 air apple astro book calendar conference designer filereference file upload Flash flex fotb08 framework ical internet internet explorer JavaScript jobs junior microsoft opportunities ria schedule singularity08 skin tutorial urlrequest web developer web development Web Standards Yahoo!

del.icio.us-ed

Stuff

Singularity?
Flex.org - The Directory for Flex
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox