플래시에서 asp.net으로 이미지를 어떻게 전달합니까?
-
06-09-2019 - |
문제
빠른 버전 :
사용자 브라우저에서 서버로 다시 생성 된 이미지를 얻으려면 어떻게해야합니까?
현재 계획은 다음과 같습니다.
- 플래시 개발자는 비트 맵을 JPEG로 변환합니다
- 그런 다음 JPEG를 사이트의 페이지에 게시합니다.
- 나는 내가 만들 수 있다고 생각한다
WebService
aStreamReader
게시물을 읽고 파일로 저장하려면.
그게 작동할까요? 이 작업을위한 기존 코드/샘플이 있습니까?
ASP.NET에 파일 업로드를 수행 할 코드를 볼 수 있어야한다고 생각합니다.
해결책
이 예에서는 무대에 버튼이있는 플래시 파일을 만들었습니다. 해당 버튼을 클릭하면 플래시가 버튼 이미지를 ASPX 파일로 보냅니다. 당신이 알듯이, 이것은 그것을 그려서 이루어집니다 DisplayObject
a BitmapData
따라서 객체를 통해 버튼에 대한 참조를 상속하는 모든 것으로 쉽게 교체 할 수 있습니다. DisplayObject
(페인트 응용 프로그램 등의 캔버스가 포함 된 영화 클립 포함).
플래시 요소를 먼저 안내 한 다음 .NET 백엔드를 안내합니다.
플래시
Flash에서 ASP.NET (또는 기타 백엔드)으로 이와 같은 생성 된 이미지를 보내려면 두 개의 제 3 자 라이브러리가 필요합니다. AS3 Core LIB에서 얻을 수있는 JPEG 인코더 (Flash가 없지만 최근의 Flex Do)가 필요합니다. http://code.google.com/p/as3corelib/. 또한 와이어를 통해 데이터를 전송하려면 Base64 인코더가 필요합니다. Dynamic Flash에서 사용 가능한 것을 사용하겠습니다. http://dynamicflash.com/goodies/base64/.
이것들을 다운로드하고 하드 디스크에서 현명한 곳 (c : lib 폴더)에서 합리적으로 추출하십시오.
새 AS3 플래시 파일을 만들어 다음으로 저장했습니다. Uploader.fla. 스테이지에 버튼 구성 요소를 추가하고 이름을 지정했습니다. btnupload. 다음으로 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 옆 에이 파일을 저장했습니다. 업 로더.
SWF를 ASP.NET 웹 사이트의 루트에 게시했습니다. 이 코드는 품질이 100%로 JPEG를 업로드하려고한다고 가정하고 데이터를 수신 할 스크립트는 UPLOADE.ASPX라고하며 사이트의 루트에 있습니다.
asp.net
내 웹 사이트의 루트에서 upload.aspx라는 웹 포름을 만들었습니다. .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");
}
}
저장 경로와 같은 하드 코딩 된 값이 분명히 있지만 이로부터 필요한 시스템을 만들 수 있어야합니다.
다른 팁
바이트 [] 또는 게시 된 파일의 스트림을 얻을 수있는 한 이미지를 조작 해야하는 경우 이미지를 만들 수 있습니다.
MemoryStream mstr = new MemoryStream(myByteArray);
Image myImage = Image.FromStream(mstr);
그는 표준 HTML 양식처럼 파일을 게시하도록하십시오. 다음 컬렉션을 사용하여 게시하는 페이지의 Page_load 이벤트에서 해당 파일에 액세스 할 수 있습니다.
request.files
이것은 FileUpload 컨트롤과 마찬가지로 HTTPPostedFiles 모음을 반환합니다.