欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

flex利用webservice上传照片实现代码

程序员文章站 2022-03-21 23:28:10
webservice端代码 复制代码 代码如下: /// /// 上传文件到远程服务器 /// /// &...
webservice端代码
复制代码 代码如下:

/// <summary>
/// 上传文件到远程服务器
/// </summary>
/// <param name="filebytes">文件流</param>
/// <param name="filename">文件名</param>
/// <returns>字符串</returns>
[webmethod(description = "上传文件到远程服务器.")]
public string uploadfile(byte[] filebytes, string filename)
{
try
{
memorystream memorystream = new memorystream(filebytes); //1.定义并实例化一个内存流,以存放提交上来的字节数组。
filestream fileupload = new filestream(server.mappath(".") + "\\" + filename, filemode.create); ///2.定义实际文件对象,保存上载的文件。
memorystream.writeto(fileupload); ///3.把内存流里的数据写入物理文件
memorystream.close();
fileupload.close();
fileupload = null;
memorystream = null;
return "文件已经上传成功";
}
catch (exception ex)
{
return ex.message;
}
}

flex客户端代码
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<s:application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minwidth="955" minheight="600" creationcomplete="application1_creationcompletehandler(event)">
<fx:script>
<![cdata[
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
{
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
{
alert.show(event.fault.tostring());
}

protected function webservice_successhandler(event:resultevent):void
{
alert.show(event.result.tostring());
}

]]>
</fx:script>
<fx:declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:webservice id="webservice" wsdl="http://10.19.1.48/upimg/service1.asmx?wsdl" fault="webservice_faulthandler(event)">
<s:operation name="uploadfile" result="webservice_successhandler(event)"></s:operation>
</s:webservice>
</fx:declarations>
<mx:image id="imgid" x="186" y="103" width="583" height="397" source="file:/g:/360云盘/照片/2013beijing mapofsubway.jpg"/>
</s:application>