about meWedia Ajax
Flex图片上传——ColdFusion篇 ... Bing ... Flex ... 0 /1541 ... 1 year 4 months ago
前段时间做了不少关于Flex方面的开发,其中涉及到Flex电子签名的图片上传问题还有DataGrid的高级渲染,今天先讲解一下Flex的图片上传功能,以后有机会再讲解如何在DataGrid里渲染任何你想要控件。

现在,假设你在一个名为“canvas”的Canvas控件,你在上面画了一幅画,然后想将这个Canvas里面的画上传到服务器端,考虑到后台使用ColdFusion的比较多,我这这里也就使用ColdFusion作为WebService了,该WebService的名为:http://localhost/ColdFusion/UploadImage.cfc?WSDL 。如果谁需要ASP.NET的,可以在下面留言。具体解决方法如下:

ActionScript代码:
protected function SaveImage():void
{
    var imageData:BitmapData = new BitmapData(this.canvas.width, this.canvas.height);
	
    imageData.draw(this.canvas);
	
    // Add digital signature
    var now:Date = new Date();
    var text:TextField = new TextField();
    text.text = now.toString();
    imageData.draw(text,new Matrix(1, 0, 0 ,1 ,160, 120), new ColorTransform(1,1,1,0.6,0.4));
	
    // Get bytes stream
    var bytes:ByteArray = new ByteArray();
    bytes = PNGEncoder.encode(imageData);  			
	
    var imageName:String = "Canvas.png";
	
    // Upload as png format image
    this._ws = new WebService();
    this._ws.wsdl = "http://localhost/ColdFusion/UploadImage.cfc?WSDL";
    var saveImage:AbstractOperation = this._ws.getOperation("SaveImage");
    saveImage.send(imageName, bytes);
}
注意上面代码中的“PNGEncoder”函数,该函数是Adobe提供的,如果您将其上传为JPG格式的图片,还请去其官方网站下载相关解码代码。下载:PNGEncoder

ColdFusion端代码:
<cfcomponent hint="Save a image uploaded from flash" output="false">
    <cffunction name="SaveImage" returntype="string" output="false" access="remote">
	
    <cfargument name="imageName" type="string" required="yes">
    <cfargument name="imageData" type="binary" required="yes">
    <cfobject name="stream" action="create" type="java" class="java.io.FileOutputStream">
    <cfset stream.init(expandPath(arguments.imageName))>
    <cfset stream.write(imageData)>
    <cfset stream.close()>
    <cfreturn arguments.imageName>
	
    </cffunction>
</cfcomponent>
OK,可以测试了。如果还有什么问题,请留言。
Name*
Email
Website
BoldItalicUnderlineJustify LeftJustify CenterJustify RightIndentOutdentBulled ListNumbered ListInsert LineCreate LinkUnlinkInsert Face
Submit