2009-05-29 3 views
7

빠른 버전 :플래시에서 ASP.NET으로 이미지를 전달하는 방법은 무엇입니까?

사용자 브라우저에서 생성 된 이미지를 서버로 다시 가져 오는 방법은 무엇입니까?

현재의 계획은 이것이다 :

  1. 플래시 개발자는 그는 다음 사이트의 페이지에 JPEG를 게시 할 예정 JPEG
  2. 에 비트 맵 변환합니다.
  3. 을 사용하여 게시물을 읽고 파일로 저장하는 WebService을 만들 수 있습니다.

그래도 될까요? 이 작업을 수행하기위한 기존 코드/샘플은 무엇입니까?

ASP.NET에 파일을 업로드하는 코드를 볼 수 있어야한다고 생각합니다.

+0

이 AS2 또는 AS3입니까? –

+0

이것은 AS3입니다. – EfficionDave

답변

13

이 예제에서는 무대에 버튼이있는 플래시 파일을 만들었습니다. 이 버튼을 클릭하면 Flash가 단추의 이미지를 ASPX 파일로 보내어 ASPX 파일로 저장합니다. 위의 그림에서 보듯이 DisplayObjectBitmapData 객체로 그려서 수행하므로 버튼에 대한 참조를 DisplayObject에서 상속되는 모든 것으로 쉽게 바꿀 수 있습니다 (페인트 응용 프로그램 용 캔버스가 포함 된 무비 클립 포함) .

먼저 플래시 요소를 살펴본 다음 .NET 백엔드를 살펴 보겠습니다.

플래시

는 ASP.NET (또는 다른 백엔드)는 제 3의 라이브러리의 몇 필요 해요로 플래시에서 다음과 같이 생성 된 이미지를 보낼 수 있습니다. 우리는 AS3 코어 라이브러리 http://code.google.com/p/as3corelib/에서 얻을 수있는 JPEG 인코더 (플래시에는 없지만 최근 버전의 Flex)가 필요합니다. 또한 와이어를 통해 데이터를 전송하기 위해 base64 인코더가 필요합니다. 동적 플래시의 플래시를 사용하겠습니다 (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); 
     } 
    } 
} 

내가 이름 Uploader.as으로 FLA 옆에이 파일을 저장.

나는 나의 Asp.NET 웹 사이트의 루트에 SWF를 발표했다. 이 코드는 100 % 품질로 jpeg를 업로드하고 데이터를받을 스크립트를 upload.aspx라고하며 사이트의 루트에 있다고 가정합니다. 내 웹 사이트의 루트에

ASP.NET 나는 upload.aspx라는 이름의 WebForm의를 만들었습니다. .aspx 파일에서 페이지 지시문과 다른 모든 내용을 제거했습니다.

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"); 
    } 
} 

같은 경로를 저장하지만,이에서 당신이 할 수 있어야로 하드 코딩 된 값이 분명히 있습니다 코드 숨김에서 다음

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %> 

, 나는 다음과 같은 추가 : 그것은이 같은 내용 모습입니다 필요한 모든 시스템을 만들 수 있습니다.

+0

는 ,,, 당신이 dynamicflash.com에서 Base64로 인코더 다운로드를 위해 제공되는 링크는 몇 가지 대안 options.Thanks에게나요 당신이 구글의 "ActionScript Base64로 인코더"에 대한 – freebird

+0

@freebird을 제안 할 수있는, 내가 생각 작동하지 않는 것 같다? 요즘 AS3에 내장 된 것 같습니다 ... –

+0

,,, www.sociodox.com에서 다운로드 했으니 까. – freebird

0

표준 HTML 양식과 같은 파일을 게시하도록하십시오. 당신은 단지는 FileUpload 컨트롤이 무엇처럼 HttpPostedFiles의 컬렉션을 반환합니다 다음 수집을

Request.Files

이를 사용하여에 자신이 게시되는 페이지의 Page_Load 이벤트에서 해당 파일을 액세스 할 수 있습니다.

1

이미지를 조작해야하는 경우 POST 파일의 바이트 [] 또는 스트림을 가져올 수있는 한 이미지를 조작 할 수 있습니다.

+0

일단 이미지를 myImage에 저장했거나 SQL에 저장하거나 이미지 컨트롤에서 사용자에게 보여줄 수 있습니까? – freebird

관련 문제