如何将图像从 Flash 传递到 ASP.NET?
-
06-09-2019 - |
题
快速版本:
如何将用户浏览器上生成的图像返回到服务器?
目前的计划是这样的:
- Flash 开发人员会将位图转换为 JPEG
- 然后,他会将 JPEG 发布到网站的页面上。
- 我想我可以创建一个
WebService
这将使用一个StreamReader
阅读帖子并将其保存为文件。
那行得通吗?有任何现有的代码/示例可以执行此操作吗?
我想我们应该能够查看将任何文件上传到 ASP.NET 的代码。
解决方案
在此示例中,我创建了一个在舞台上带有按钮的 Flash 文件。当您单击该按钮时,Flash 会将按钮的图像发送到 ASPX 文件,该文件将其保存为 JPEG。正如您将看到的,这是通过绘制 DisplayObject
变成一个 BitmapData
对象,因此,您可以轻松地用继承自的任何内容替换对按钮的引用 DisplayObject
(包括包含用于绘画应用程序等的画布的影片剪辑)。
我将首先引导您了解 Flash 元素,然后介绍 .NET 后端。
闪光
要将这样生成的图像从 Flash 发送到 ASP.NET(或任何其他后端),您将需要一些第 3 方库。我们需要一个 JPEG 编码器(Flash 没有,但 Flex 的最新版本有),我们可以从 AS3 Core Lib 获取 http://code.google.com/p/as3corelib/. 。我们还需要一个 Base64 编码器来通过线路发送数据。我将使用 Dynamic Flash 中的一个,可在 http://dynamicflash.com/goodies/base64/.
下载这些文件并将它们解压到硬盘上的某个合适位置(例如 C:\lib 文件夹)。
我创建了一个新的 AS3 Flash 文件并将其另存为 上传者.fla. 。我在舞台上添加了一个按钮组件并将其命名 按钮上传. 。接下来,我编辑了 ActionScript 设置并添加了我的 c:\lib 文件夹到类路径。然后我给文档起了一个类名 上传者 并保存文件。
接下来,我创建了一个 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 旁边,名称为 上传者.as.
我将 SWF 发布到我的 Asp.NET 网站的根目录中。此代码假设您要上传质量为 100% 的 jpeg,并且接收数据的脚本名为 upload.aspx,位于网站的根目录中。
网络平台
在我网站的根目录中,我创建了一个名为 upload.aspx 的 WebForm。在 .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");
}
}
显然有硬编码的值,例如保存路径,但从中您应该能够创建您需要的任何系统。
其他提示
如果需要操作图像,只要能获取 POSTed 文件的 byte[] 或 Stream,就可以创建它的图像,例如
MemoryStream mstr = new MemoryStream(myByteArray);
Image myImage = Image.FromStream(mstr);
让他像标准 HTML 表单一样发布文件。您可以使用以下集合在他要发布到的页面的 Page_Load 事件中访问这些文件
请求文件
这将返回 HttpPostedFiles 的集合,就像 FileUpload 控件所做的那样。