この例では、ステージ上のボタンを含む Flash ファイルを作成しました。そのボタンをクリックすると、Flash はボタンの画像を ASPX ファイルに送信し、JPEG として保存します。DisplayObject
をオブジェクトに描画することでこれが行われることがわかるBitmapData
ように、ボタンへの参照を継承するものDisplayObject
(ペイント アプリケーションのキャンバスを含むムービー クリップなど) に簡単に置き換えることができます。
最初に Flash 要素について説明し、次に .NET バックエンドについて説明します。
閃光
このように生成されたイメージを Flash から ASP.NET (またはその他のバックエンド) に送信するには、いくつかのサードパーティ ライブラリが必要になります。AS3 Core Lib http://code.google.com/p/as3corelib/から取得できる JPEG エンコーダー (Flash にはありませんが、最近のバージョンの Flex にはあります) が必要です。データをネットワーク経由で送信するための base64 エンコーダーも必要です。http://dynamicflash.com/goodies/base64/で入手できる Dynamic Flash のものを使用します。
これらをダウンロードして、ハードディスク上の適切な場所 (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 Web サイトのルートに公開しました。このコードは、100% の品質で jpeg をアップロードすること、およびデータを受け取るスクリプトが upload.aspx と呼ばれ、サイトのルートにあることを前提としています。
ASP.NET
Web サイトのルートに、upload.aspx という名前の Web フォームを作成しました。.aspx ファイルで、ページ ディレクティブ以外のすべてのコンテンツを削除しました。その内容は次のようになります。
<%@ 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");
}
}
保存パスなどの値が明らかにハードコードされていますが、これから必要なシステムを作成できるはずです。