首先作为一个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大手提供断点续传思路。。。。。。。。。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。