Upload file to server in as3

2019-06-13 23:21发布

问题:

How to upload a File to server in as3? I don't want to browse file by using FileReference.browse(). I tried using URLLoader to get the file and convert to byteArray but as I send it to server using URLVariables it gives IOError=2032. I want update database on the server. The structure would be like email (string) sqlite_db (Blob)

I have to send both of these variables inside one request. Any idea!

回答1:

The upload process is made much easier by using the UploadPostHelper which is a great class to create multipart data forms:

ByteArray byteArray = [YOUR FILE DATA];

var urlRequest : URLRequest = new URLRequest();
urlRequest.url = 'http://your.server.com/destination';
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = 
    UploadPostHelper.getPostData( 
        'filename.ext', 
        byteArray, 
        { 
            email:"emailParameter", 
            other:"otherParameter" 
        } );
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );

// create a loader & send the file to the server;
var urlLoader : URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load( urlRequest );

And of course you can listen to the complete event and pass back any information from your server. Hope that helps.