快速版本:
如何将在用户浏览器上生成的图像返回到服务器?
目前的计划是这样的:
- Flash 开发人员会将位图转换为 JPEG
- 然后,他会将 JPEG 文件发布到网站上的页面。
- 我在想我可以创建一个
WebService将使用 aStreamReader来阅读帖子并将其保存为文件的内容。
那行得通吗?这样做的任何现有代码/示例?
我想我们应该能够查看将任何文件上传到 ASP.NET 的代码。
快速版本:
如何将在用户浏览器上生成的图像返回到服务器?
目前的计划是这样的:
WebService将使用 aStreamReader来阅读帖子并将其保存为文件的内容。那行得通吗?这样做的任何现有代码/示例?
我想我们应该能够查看将任何文件上传到 ASP.NET 的代码。
在此示例中,我在舞台上创建了一个带有按钮的 Flash 文件。当您单击该按钮时,Flash 会将按钮的图像发送到一个 ASPX 文件,该文件将其保存为 JPEG。正如您将看到的,这是通过将 绘制DisplayObject到BitmapData对象中来完成的,因此,您可以轻松地将按钮的引用替换为继承自的任何内容DisplayObject(包括包含用于绘画应用程序的画布的影片剪辑等)。
我将先向您介绍 Flash 元素,然后再介绍 .NET 后端。
闪光
要将这样的生成图像从 Flash 发送到 ASP.NET(或任何其他后端),您将需要几个 3rd 方库。我们需要一个 JPEG 编码器(Flash 没有,但最新版本的 Flex 有),我们可以从 AS3 Core Lib http://code.google.com/p/as3corelib/ 获得它。我们还需要一个 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 网站的根目录中。此代码假定您要上传质量为 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");
}
}
显然有硬编码的值,例如保存路径,但您应该能够从这里创建您需要的任何系统。
如果需要对图片进行操作,只要能拿到POST文件的byte[]或者Stream,就可以创建它的图片,例如
MemoryStream mstr = new MemoryStream(myByteArray);
Image myImage = Image.FromStream(mstr);
让他像标准 HTML 表单一样发布文件。您可以使用以下集合在他发布到的页面的 Page_Load 事件中访问这些文件
请求文件
这将返回 HttpPostedFiles 的集合,就像 FileUpload 控件所做的一样。