POST file upload using URLRequest

13,347

You should add more info to the "Content-Type" header:

uploadRequest.contentType = "multipart/form-data; boundary=<<boundary here>>";
Share:
13,347
Tristan Shelton
Author by

Tristan Shelton

Updated on June 08, 2022

Comments

  • Tristan Shelton
    Tristan Shelton almost 2 years

    I have a quick question regarding POST file uploads in ActionScript 3. I am trying to upload a ByteArray from memory via POST to a server. I'm using the URLRequest class to send the data, and URLLoader because I want to monitor the progress. The relevant sections of code follows:

    var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php");
    uploadRequest.method = URLRequestMethod.POST;
    uploadRequest.contentType = "multipart/form-data";
    uploadRequest.data = myByteArray;
    
    var uploader:URLLoader = new URLLoader;
    uploader.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
    uploader.addEventListener(Event.COMPLETE, onUploadComplete);
    uploader.dataFormat = URLLoaderDataFormat.BINARY;
    uploader.load(uploadRequest);
    

    The problem is that I've set my callbacks to trace the upload progress, and the bytesTotal property of the ProgressEvent is always 1960 (the size of the request minus data?), even though the actual data is around 20MB and no file is uploaded even after the Complete event fires.

    I've verified that upload.php functions correctly with a simple html form, and I can verify that myByteArray contains all of the data in question. Can anyone tell me what I'm doing wrong?

    Edit:

    I've attempted a couple of new things that I thought I should mention. The first is setting the content type to application/octet-stream instead of multipart/form-data, which had no effect other than increasing the number of bytes to 1964. I also checked the Apache error log and found the following text repeated a lot:

    PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

    I'm guessing that Flash isn't formatting the HTTP request properly for whatever reason. Given that I created a FileReference that makes use of the same methods I set for the URLLoader to upload a file from disk, and got the expected result: the bytesTotal property matched the file size and the file was uploaded correctly.

    Working from that I found a page in the Adobe developer docs that mentions uploading data to a server using FileReference.upload() by setting the data parameter of the URLRequest, so I tried the following code:

    var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php");
    uploadRequest.method = URLRequestMethod.POST;
    uploadRequest.data = myByteArray;
    
    fileRef = new FileReference;
    fileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
    fileRef.addEventListener(Event.COMPLETE, onUploadComplete);
    fileRef.upload(uploadRequest);
    

    Which resulted in the following output:

    ArgumentError: Error #2127: FileReference POST data cannot be type ByteArray.

    I'm really stuck here. Any suggestions would be appreciated!

  • Tristan Shelton
    Tristan Shelton about 12 years
    That worked! Thank you so much! I assumed that Flash would add the boundaries for me. No wonder it wasn't working.