在本例中,我在舞台上创建了一个带有按钮的 Flash 文件。当您单击该按钮时,Flash 会将按钮的图像发送到一个 ASPX 文件,该文件将其保存为 JPEG。正如您将看到的,这是通过将DisplayObject 绘制到BitmapData 对象中来完成的,因此,您可以轻松地将按钮的引用替换为从DisplayObject 继承的任何内容(包括包含画布的影片剪辑)用于油漆应用等)。
我将先向您介绍 Flash 元素,然后再介绍 .NET 后端。
Flash
要将这样的生成图像从 Flash 发送到 ASP.NET(或任何其他后端),您将需要几个 3rd 方库。我们需要一个 JPEG 编码器(Flash 没有,但最新版本的 Flex 有),我们可以从 AS3 Core Lib http://code.google.com/p/as3corelib/ 获得它。我们还需要一个 base64 编码器来通过网络发送数据。我将使用来自 Dynamic Flash 的那个,地址为http://dynamicflash.com/goodies/base64/。
下载这些文件并将它们解压缩到硬盘上的某个合适的位置(如 C:\lib 文件夹)。
我创建了一个新的 AS3 Flash 文件并将其保存为 uploader.fla。我在舞台上添加了一个按钮组件并将其命名为 btnUpload。接下来,我编辑了 ActionScript 设置并将我的 c:\lib 文件夹添加到类路径中。然后我给文档一个类名 Uploader 并保存文件。
接下来,我创建了一个 ActionScript 文件并向其中添加了以下代码:
package
{
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.utils.ByteArray;
import fl.controls.Button;
import com.adobe.images.JPGEncoder;
import com.dynamicflash.util.Base64;
public class Uploader extends MovieClip
{
// Reference to the button on the stage
public var btnUpload:Button;
// Encoder quality
private var _jpegQuality:int = 100;
// Path to the upload script
private var _uploadPath:String = "/upload.aspx";
public function Uploader()
{
btnUpload.addEventListener(MouseEvent.CLICK, buttonClick);
}
private function buttonClick(e:MouseEvent):void
{
// Create a new BitmapData object the size of the upload button.
// We're going to send the image of the button to the server.
var image:BitmapData = new BitmapData(btnUpload.width, btnUpload.height);
// Draw the button into the BitmapData
image.draw(btnUpload);
// Encode the BitmapData into a ByteArray
var enc:JPGEncoder = new JPGEncoder(_jpegQuality);
var bytes:ByteArray = enc.encode(image);
// and convert the ByteArray to a Base64 encoded string
var base64Bytes:String = Base64.encodeByteArray(bytes);
// Add the string to a URLVariables object
var vars:URLVariables = new URLVariables();
vars.imageData = base64Bytes;
// and send it over the wire via HTTP POST
var url:URLRequest = new URLRequest(_uploadPath);
url.data = vars;
url.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.load(url);
}
}
}
我将此文件保存在 FLA 旁边,名称为 Uploader.as。
我将 SWF 发布到我的 Asp.NET 网站的根目录中。
此代码假定您要上传质量为 100% 的 jpeg,并且将接收数据的脚本称为 upload.aspx,并且位于站点的根目录中。
ASP.NET
在我网站的根目录中,我创建了一个名为upload.aspx 的WebForm。在 .aspx 文件中,我删除了除 page 指令之外的所有内容。它的内容是这样的:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
然后在 CodeBehind 中,我添加了以下内容:
using System;
using System.IO;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Get the data from the POST array
string data = Request.Form["imageData"];
// Decode the bytes from the Base64 string
byte[] bytes = Convert.FromBase64String(data);
// Write the jpeg to disk
string path = Server.MapPath("~/save.jpg");
File.WriteAllBytes(path, bytes);
// Clear the response and send a Flash variable back to the URL Loader
Response.Clear();
Response.ContentType = "text/plain";
Response.Write("ok=ok");
}
}
显然有硬编码的值,例如保存路径,但您应该能够创建所需的任何系统。