How does ActionScript upload files to the server?

Source: Internet
Author: User
Tags creative commons attribution http post
Preface

Recently looked back to the former Faust enhanced version, found that the previous image upload method is not a specification, so here again took a copy of the online code can be uploaded into the form of the file. Want to float Faustus Avatar cutting enhanced version source code, will be given later. Tool Class

Package Easis.common {Import flash.events.*;
    Import flash.net.*;
    Import Flash.utils.ByteArray;

    Import Flash.utils.Endian; /** * Take A fileName, ByteArray, and parameters object as input and return ByteArray post data suitable for a urlreq Uest as Output * * @see http://marstonstudio.com/?p=36 * @see http://www.w3.org/TR/html4/interact/forms.htm L * @see http://www.jooce.com/blog/?p=143 * @see http://www.jooce.com/blog/wp%2Dcontent/uploads/2007/06/uploadFi Le.txt * @see http://blog.je2050.de/2006/05/01/save-bytearray-to-file-with-php/* * @author Jonathan Marsto n * @version 2007.08.19 * * This work the licensed under a Creative Commons attribution noncommercial Sharea
     Like 3.0 License.
         * @see http://creativecommons.org/licenses/by-nc-sa/3.0/* */public class uploadposthelper2{/** * Boundary used to break up different parts of the HTTP POST body */private static var _boundary:string = "";
         /** * Get the boundary for the post. * Must is passed as part of the contentType of the urlrequest/public static function Getboundary (): STR
                    ing {if (_boundary.length = 0) {for (var i:int = 0; i < 0x20; i++) {
                _boundary + = string.fromcharcode (int (+ math.random () * 25));
        } return _boundary;
/////To upload the picture//var one4data:bitmap= model.scanvo.currentScanImage; 
The picture is converted to a character array/////encoded (very important)//var data:bytearray=new jpgencoder (). Encode (One4data.bitmapdata); var url:string= "http://www.test.com/upload/UploadAction.action";//upload address yourself based on actual situation//var request:urlre
quest=new urlrequest (URL); Form form submission, while declaring separator boundary//request.contenttype= "MULTIPART/FORM-DATA;"
Boundary= "+uploadposthelper.getboundary (); Request.requestHeaders.push (New URLRequestheader (' Cache-control ', ' No-cache '));
Request.method=urlrequestmethod.post; Setting up the upload file name and uploading data//////getpostdata () method is mainly based on RFC1867 to process data//REQUEST.DATA=UPLOADPOSTHELPER.GETPOSTDA
        Ta ("test.jpg", data);
        private Var _req:urlrequest;
        private Var _parameters={};
        private Var _filemap:array=[];
                    Public Function UploadPostHelper2 (url:string) {_req=new urlrequest (URL); Form form submission, while declaring delimiters boundary _req.contenttype= "multipart/form-data;"
                    Boundary= "+uploadposthelper2.getboundary ();
                    _req.requestheaders.push (New Urlrequestheader (' Cache-control ', ' No-cache '));          
        _req.method=urlrequestmethod.post;
         }/*** * Add parameters to go in.
        * ***/Public Function Addparameter (key:string,value:string) {_parameters[key]=value;
         /**** * Add the contents of the file in. * ***/Public Function addFile (Filename:string,uploadfieldname:string,filecontent:bytearray) {This._filemap.push ({fileName:


        FileName, Fieldname:uploadfieldname, content:filecontent});
         /*** * Set the main part of the request.
            * ***/Private Function Setreqdata () {var i:int;

            var bytes:string;
            var postdata:bytearray = new ByteArray ();

            Change or read the byte order of the data Postdata.endian = Endian.big_endian;
            Add Filename to parameters if (this._parameters== null) {this._parameters = new Object (); }//Traverse parameters Properties//add parameters to PostData for (Var name:string in t
                his._parameters) {postdata = boundary (postdata);
                PostData = LineBreak (postdata); bytes = ' Content-disposition:form-data;
                Name= "' + name + '"; for (i = 0; i < byteS.length;
                i++) {postdata.writebyte (Bytes.charcodeat (i));
                } PostData = LineBreak (postdata);
                PostData = LineBreak (postdata);
                Postdata.writeutfbytes (This._parameters[name]);
            PostData = LineBreak (postdata);
                for (var j=0;j< this._filemap.length;j++) {var fileitem=this._filemap[j];
                var uploaddatafieldname=fileitem.fieldname;
                var filename=fileitem.filename;
                var filecontent=fileitem.content;
                Add Filedata to PostData postdata = boundary (postdata);
                PostData = LineBreak (postdata); bytes = ' Content-disposition:form-data; Name= "' +uploaddatafieldname+ '";
                Filename= "';
                for (i = 0; i < bytes.length i++) {postdata.writebyte (Bytes.charcodeat (i)); } postdata.writeutfbyteS (fileName);
                PostData = Quotationmark (postdata);
                PostData = LineBreak (postdata);
                bytes = ' Content-type:application/octet-stream ';
                for (i = 0; i < bytes.length i++) {postdata.writebyte (Bytes.charcodeat (i));
                } PostData = LineBreak (postdata);
                PostData = LineBreak (postdata);
                Postdata.writebytes (filecontent, 0, filecontent.length);
            PostData = LineBreak (postdata);
            }//closing Boundary postdata = boundary (postdata);        
            PostData = Doubledash (postdata);

        _req.data=postdata;
            Public Function Geturlrequest (): urlrequest {this.setreqdata ();
        return this._req; }/** * Add a boundary to the PostData with leading Doubledash add a separator starting with a double dash * * * Priva Te static function BounDary (P:bytearray): ByteArray {var l:int = uploadposthelper2.getboundary (). length;
            p = Doubledash (p);
            for (var i:int = 0; i < L; i++) {P.writebyte (_boundary.charcodeat (i));
        } return p; /** * Add one linebreak adding a blank line * * private static function LineBreak (P:bytearray): Bytea
            Rray {p.writeshort (0x0d0a);
        return p; /** * Add quotation mark quotes */private static function Quotationmark (P:bytearray): B
            Ytearray {p.writebyte (0x22);
        return p; /** * Add Double Dash--*/private static function Doubledash (P:bytearray): byt
            Earray {p.writeshort (0x2d2d);
        return p; }

    }

}
How to use
            Generate Code container var jpgencoder:jpgencoder = new Jpgencoder (100);
            Encoding the bitmap data into the container becomes the ByteArray stream//var Jpgstream:bytearray = Jpgencoder.encode (_SRCBMD);
            var jpgstream = new ByteArray ();

            Jpgstream.writebytes (Jpgencoder.encode (_NEWBMD));
            var jpgsrcstream=new bytearray ();

            Jpgsrcstream.writebytes (Jpgencoder.encode (_SRCBMD));

            var uploadposthelper=new UploadPostHelper2 (_uploadurl);
            Uploadposthelper.addfile ("Thumb.jpg", Param.imageuploadkey,jpgstream);
            if (PARAM.UPLOADSRC) {uploadposthelper.addfile ("src.jpg", Param.imagesrcuploadkey,jpgsrcstream);
            var jpgurlrequest:urlrequest = Uploadposthelper.geturlrequest ();
            var loader:urlloader = new Urlloader ();
            Loader.addeventlistener (Event.complete, this.uploadcomplete);
            Loader.addeventlistener (Event.complete, this.uploadcomplete); Loader.addevEntlistener (Ioerrorevent.io_error, This.errorhandler);      Loader.load (jpgurlrequest);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.