Products
Technologies

Developer resources

Upload multiple files using URLLoader by 1 user action

Avg. Rating 5.0

Problem

After generating an images for preview and thumbnail, using ImageSnapshot, I was need to upload them to a server. But when I was try to upload them one another, Flash Player Security start to demand user action on each download action, not only for first.

Solution

Was created a class to construct multipart/form-data request with more than 1 file data in it, and upload several files using 1 request and 1 user action.

Detailed explanation

Main target of this class is to upload mutiple data by one user action.
  
Takes an array with prepared DTO as input;
Returns ByteArray to use as post data in UrlRequest as output; ByteArray is formatter as multipart/form-data request,
as format is similar to FireFox request.
 
DTO can be generated via POSTUploadBuilder.buildUploadDataVO method.

DTO structure:
 fileName : String;
 fileData : ByteArray;
 dataName : String;

Usage example:

           var request : URLRequest = new URLRequest(
"http://somehost/upload  );
                      
            request.contentType    = 'multipart/form-data;
boundary=' + POSTUploadBuilder.boundary;
            request.method        = URLRequestMethod.POST;
          
            var arr : Array = [];
          
            for ( var i:uint = 0; i < fileList.length; ++i )
            {
                arr.push( POSTUploadBuilder.buildUploadDataVO(
fileList[i].name, fileList[i].data, fileList[i].name ) );
            }          
           
            request.data        = POSTUploadBuilder.buildPOSTData(
arr );
           
            var loader : URLLoader = new URLLoader();
           
            loader.addEventListener( Event.COMPLETE,
onUploadComplete );
            loader.addEventListener( IOErrorEvent.IO_ERROR,
onUploadError );
            loader.addEventListener(
SecurityErrorEvent.SECURITY_ERROR, onUploadError );
           
            loader.dataFormat = URLLoaderDataFormat.BINARY;
           
            loader.load( request );
com.zip
[source]

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes