actionscript里面如何上传文件到服务端?

时间:2021-10-04 13:36:25

前言

最近看回以前的浮士德增强修改版,发现以前的图片上传方式不是规范,所以这里又拿了网上的一份代码改成了可以上传文件的形式。想要浮士德头像裁剪增强版源代码的话,后面会给出来的。

工具类

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 UrlRequest as output * * @see http://marstonstudio.com/?p=36 * @see http://www.w3.org/TR/html4/interact/forms.html * @see http://www.jooce.com/blog/?p=143 * @see http://www.jooce.com/blog/wp%2Dcontent/uploads/2007/06/uploadFile.txt * @see http://blog.je2050.de/2006/05/01/save-bytearray-to-file-with-php/ * * @author Jonathan Marston * @version 2007.08.19 * * This work is licensed under a Creative Commons Attribution NonCommercial ShareAlike 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 be passed as part of the contentType of the UrlRequest */
        public static function getBoundary():String {

            if(_boundary.length == 0) {
                for (var i:int = 0; i < 0x20; i++ ) {
                    _boundary += String.fromCharCode( int( 97 + Math.random() * 25 ) );
                }
            }

            return _boundary;
        }
// //要上传的图片
// var one4data:Bitmap= model.scanvo.currentScanImage;
// //图片转换成字符数组
// //对传送数据编码(很重要)
// var data:ByteArray=new JPGEncoder().encode(one4data.bitmapData); 
// 
// var url:String="http://www.test.com/upload/UploadAction.action";//上传地址自己根据实际情况定
// var request:URLRequest=new URLRequest(url);
// //form表单提交,同时声明分隔符boundary
// request.contentType="multipart/form-data; boundary="+UploadPostHelper.getBoundary();
// request.requestHeaders.push(new URLRequestHeader( 'Cache-Control', 'no-cache'));
// request.method=URLRequestMethod.POST;
// //设置上传文件名和上传数据
// 
// //getPostData()方法主要是根据RFC1867来处理数据
// request.data=UploadPostHelper.getPostData("test.jpg",data );
        private var _req:URLRequest;
        private var _parameters={};
        private var _fileMap:Array=[];
        public function  UploadPostHelper2(url:String){
            _req=new URLRequest(url);
                    //form表单提交,同时声明分隔符boundary
                    _req.contentType="multipart/form-data; boundary="+UploadPostHelper2.getBoundary();
                    _req.requestHeaders.push(new URLRequestHeader( 'Cache-Control', 'no-cache'));
                    _req.method=URLRequestMethod.POST;          
        }
        /*** * 添加参数进去。 * ***/
        public function addParameter(key:String,value:String){
            _parameters[key]=value;
        }
        /**** * 添加文件内容进去。 * ***/
        public function addFile(fileName:String,uploadFieldName:String,fileContent:ByteArray){
            this._fileMap.push({
            fileName:fileName
            ,fieldName:uploadFieldName
            ,content:fileContent
            });


        }
        /*** * 设定request的主体部分。 * ***/
        private function setReqData(){
            var i: int;
            var bytes:String;

            var postData:ByteArray = new ByteArray();
            //更改或读取数据的字节顺序
            postData.endian = Endian.BIG_ENDIAN;

            //add Filename to parameters
            if(this._parameters== null) {
                this._parameters = new Object();
            }

            //遍历parameters中的属性
            //add parameters to postData
            for(var name:String in this._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 添加以双破折号开始的分隔符 */
        private 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 添加空白行 */
        private static function LINEBREAK(p:ByteArray):ByteArray {
            p.writeShort(0x0d0a);
            return p;
        }

        /** * Add quotation mark 添加引号 */
        private static function QUOTATIONMARK(p:ByteArray):ByteArray {
            p.writeByte(0x22);
            return p;
        }

        /** * Add Double Dash 添加双破折号-- */
        private static function DOUBLEDASH(p:ByteArray):ByteArray {
            p.writeShort(0x2d2d);
            return p;
        }

    }

}

使用方式

            //生成编码容器
            var jpgEncoder:JPGEncoder = new JPGEncoder(100);
            //将位图数据编码到容器内成为ByteArray流
            //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);