1. ホーム
  2. Web プログラミング
  3. フレックス

フレックスはウェブサービスを使用して写真をアップロードし、コードを実装します。

2022-01-19 22:55:38
WebService側のコード
コピーコード コードは以下の通りです。

///
/// Uploading files to a remote server
///

/
/// FileStream
/// file name
/// String
[WebMethod(Description = "Upload a file to a remote server...")]
public string UploadFile(byte[] fileBytes, string fileName)
{{br
try
{{br
MemoryStream memoryStream = new MemoryStream(fileBytes); //1. Define and instantiate a memory stream to hold the array of bytes submitted up.
FileStream fileUpload = new FileStream(Server.MapPath(".") + "\\" + fileName, FileMode.Create); /// 2. Define the actual file object that holds the uploaded file.
memoryStream.WriteTo(fileUpload); ////3. Write the data in the memory stream to the physical file
memoryStream.Close();
fileUpload.Close();
fileUpload = null; {
memoryStream = null;
return "File has been uploaded successfully";
}
catch (Exception ex)
{{br
return ex.Message;
}
}

Flexクライアントコード
コピーコード コードは以下の通りです。

/{br

xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event )">


import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.graphics.codec.JPEGEncoder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{{br
var width :int = imgID.width;
var height :int = imgID.height;
var bitmapData:BitmapData =new BitmapData(width,height);
bitmapData.draw(imgID);
{
var byteArr:ByteArray = bitmapData.getPixels(new Rectangle(0,0,width,height));
var byteArr123:ByteArray =new JPEGEncoder().encodeByteArray(byteArr,width,height);
{
webService.UploadFile(byteArr123, "123.png");
}
protected function webService_faultHandler(event:FaultEvent):void
{{br
Alert.show(event.result.toString());
}
protected function webService_successHandler(event:ResultEvent):void
{{br
Alert.show(event.result.toString());
}
]]>