Eff*_*ave 7 asp.net flash actionscript-3 image-upload
快速版:
如何将用户浏览器上生成的图像恢复到服务器?
目前的计划如下:
WebService将使用a StreamReader来阅读帖子并将其保存为文件.那会有用吗?这样做的任何现有代码/样本?
我想我们应该能够查看代码,以便将任何文件上传到ASP.NET.
Gre*_*g B 13
在这个例子中,我在舞台上创建了一个带有按钮的Flash文件.单击该按钮时,Flash会将按钮的图像发送到ASPX文件,该文件将其保存为JPEG.正如您将看到这是通过DisplayObject将BitmapData对象绘制到对象中来完成的,因此,您可以轻松地将对按钮的引用替换为继承的任何内容DisplayObject(包括包含绘图应用程序的画布的影片剪辑等).
我将首先介绍Flash元素,然后介绍.NET后端.
闪
要将生成的图像从Flash发送到ASP.NET(或任何其他后端),您将需要几个第三方库.我们需要一个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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将此文件保存在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" %>
Run Code Online (Sandbox Code Playgroud)
然后在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");
}
}
Run Code Online (Sandbox Code Playgroud)
显然存在硬编码值,例如保存路径,但是您应该能够创建所需的任何系统.
| 归档时间: |
|
| 查看次数: |
11858 次 |
| 最近记录: |