开发者社区> 问答> 正文

FLEXPOST提交文件到阿里云OSS

首先作为一个flex新手,强烈吐槽一下阿里云不提供flex Api,然后嘞说明一下目前只解决了单个文件上传,断点续传还在研究中。。。。。
再然后不废话,直接上代码。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="loads(event);">
    <fx:Script>
    <![CDATA[
    import com.base.MsMultiPartFormData;
    
    import mx.controls.Alert;
    import mx.events.FlexEvent;
    import mx.rpc.events.ResultEvent;
    private var file:FileReference = new FileReference();
    protected function fileLoad():void
    {
    file.browse();
    }
    /**
     * 初始化
     * @author ldy
     */
    protected function loads(event:FlexEvent):void
    {
    file.addEventListener(ProgressEvent.PROGRESS,progress);
    file.addEventListener(Event.SELECT,select);
    file.addEventListener(Event.COMPLETE,complete);
    }
    protected function select(e:Event):void
    {
    file.load();
    }
    
    protected function progress(e:ProgressEvent):void
    {
    
    }
    
    /**
     * 文件加载完成,如果没有授权访问,参数只提供 key 足矣
     * @author ldy
     */
    protected function complete(e: Event):void
    {
        var arrays:ByteArray = file.data;    
        var form:MsMultiPartFormData=new MsMultiPartFormData();
        form.AddFormField("OSSAccessKeyId","xxxx");
        form.AddFormField("policy","xxxx");
        form.AddFormField("signature","xxx");
        form.AddFormField("key","1.txt");
        form.AddStreamFile("file","1.txt",file.data);
        form.PrepareFormData()
        var  array:ByteArray = file.data;
        //服务
        var service:URLLoader=new URLLoader();
        var datas:MsMultiPartFormData = new MsMultiPartFormData();
        var request:URLRequest=new URLRequest("http://xxx.oss-cn-beijing.aliyuncs.com");
        var header:URLRequestHeader = new URLRequestHeader ("Content-Type", "multipart/form-data; boundary="+form.Boundary);  
        request.requestHeaders.push(header);
        request.method="POST";        
        request.data = form.GetFormData();            
        var load:URLLoader = new URLLoader(request);    
    }
    
    protected function oss_resultHandler(event:ResultEvent):void
    {
    }

    ]]>
    </fx:Script>
    <fx:Declarations>
    </fx:Declarations>
    <s:Form >
        <s:Button x="196" y="279" label="选择文件" click="fileLoad()"/>
    </s:Form>
</s:Application>

模拟web http  post multipart/form-data 以下代码大部分来源于网络 如果有兴趣可以自行百度
package com.base
{
    import flash.utils.ByteArray;

    public class MsMultiPartFormData
    {
        
        public var Boundary:String= "---------------------------7db1851cd1158";
        
        private var fieldName:String="Content-Disposition: form-data; name=\"XXXX\"";
        
        private var fieldValue:String= "XXXX";
        
        private var fileField:String="Content-Disposition: form-data; name=\"XXXX\"; filename=\"XXXXXXXX\"";
        
        private var fileContentType:String= "Content-Type: XXXX";
        
        private var formData:ByteArray;  
        //构造
        public function MsMultiPartFormData(){  formData=new ByteArray(); }
        
        /**
         * 添加一个字段数据到From的数据包中
         * @author ldy
         */
        public function AddFormField( FieldName:String,  FieldValue:String):void
        {
            var newFieldName:String=fieldName;
            var newFieldValue:String=fieldValue;
            newFieldName=newFieldName.replace("XXXX",FieldName);
            newFieldValue=newFieldValue.replace("XXXX",FieldValue);
            
            formData.writeMultiByte( "--"+Boundary+"\r\n","UTF-8");
            formData.writeMultiByte( newFieldName+"\r\n\r\n","UTF-8");
            formData.writeMultiByte( newFieldValue+"\r\n","UTF-8");
        }
        
        /**
         * 添加一个文件二进流数据到Form的数据包中,并指定二进流数据的类型
         * @author ldy
         */
        public function AddFile( FieldName:String, FileName:String,FileContent:ByteArray, ContentType:String):void
        {
            var newFileField:String=fileField;
            var newFileContentType:String=fileContentType;
            newFileField=newFileField.replace("XXXX",FieldName);
            newFileField=newFileField.replace("XXXXXXXX",FileName);
            newFileContentType=newFileContentType.replace("XXXX",ContentType);
            formData.writeMultiByte( "--"+Boundary+"\r\n","UTF-8");
            formData.writeMultiByte( newFileField+"\r\n","UTF-8");
            formData.writeMultiByte( newFileContentType+"\r\n\r\n","UTF-8");
             formData.writeBytes(FileContent,0,FileContent.length);
            formData.writeMultiByte("\r\n","UTF-8");
        }
        
        
        /**
         * 添加一个文件二进流数据到Form的数据包中
         * @author ldy
         */
        public function AddStreamFile( FieldName:String, FileName:String,FileContent:ByteArray):void
        {
            AddFile( FieldName, FileName, FileContent,"application/octet-stream");
        }
        
        /**
         * 把Form中所有的字段与二进制流数据打包成一个完整的From数据包
         * @author ldy
         */
        public function PrepareFormData():void
        {
            formData.writeMultiByte( "--"+Boundary+"--","UTF-8");
        }
        
        /**
         * 获得From的完整数据
         * @author ldy
         */
        public function GetFormData():ByteArray
        {
            return formData;
        }
        
    }
}

    然后,再然后。坐等flex大手提供断点续传思路。。。。。。。。。






展开
收起
tb8477572_2012-15516 2015-10-27 00:30:38 8388 0
2 条回答
写回答
取消 提交回答
  • ReFLEXPOST提交文件到阿里云OSS
    这样会遇到:Error #2032: 流错误。
    2017-03-06 18:34:00
    赞同 展开评论 打赏
  • 阿里云对象存储(OSS)开发工程师
    ReFLEXPOST提交文件到阿里云OSS
    2015-10-27 15:49:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载