代码大部分是别人的,自己做了些修改
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" >
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
[Bindable]
private var stateText:String = "请选择一个文件上传";
//通过调用file对象的方法来完成上传和下载功能
private var fileRef:FileReference = new FileReference();
private function initApp():void
{
var imageTypes:FileFilter = new FileFilter("图片 (*.jpg, *.jpeg, *.gif,*.png)", "*.jpg; *.jpeg; *.gif; *.png");
var textTypes:FileFilter = new FileFilter("文本文件(*.txt","*.txt;");
var officeType:FileFilter = new FileFilter("Office文件(*.doc, *.xls","*.doc; *.xls");
var anyType:FileFilter = new FileFilter("所有文件(*.*)","*.*");
//var allTypes:Array = new Array(textTypes,imageTypes, officeType,anyType);//限制上传类型
var allTypes:Array = new Array(imageTypes);
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
fileRef.addEventListener("ioError", ioerrorHandler);
try{
var success:Boolean = fileRef.browse(allTypes);
//Alert.show();
}catch (error:Error){
trace("Unable to browse for files."+error.toString());
}
}
private function ioerrorHandler(event:Event):void{
Alert.show("Unable to upload file."+event.toString());
trace("Unable to upload file."+event.toString());
}
/*
另外我们也可以不用上面这中定义一个函数在程序加载时调用进行初始化操作,应用程序(mxml)的初始化操作又creationComplete方法完成,另外还有一个比它先执行的方法createChildren(),我们可以直接在mxml下重写该方法来实现应用程序的初始化,如下:
**
* createChildren 比 creationComplete 事件更早发生
*
*/
/* protected override function createChildren():void
{
file.addEventListener(Event.SELECT,onSelected);
file.addEventListener(Event.COMPLETE,onCompleted);
file.addEventListener(ProgressEvent.PROGRESS,onProgress);
}
*/
internal function selectHandler(evt:Event):void
{
stateText = "选择了文件" + fileRef.name;
}
internal function completeHandler(evt:Event):void
{
stateText = "上传完毕!";
}
internal function progressHandler(evt:ProgressEvent):void
{
stateText = "已上传 " + Math.round(100 * evt.bytesLoaded / evt.bytesTotal) + "%";
}
/**
* 调用FileReference的实例方法upload()实现文件上传
* 到这里客户端就只差一步了,那就是完成发起上传请求的方法,实际上就是通过URLRequest对象创建一个与服务端的连接,
* 然后直接调用FielReference类的upload()方法就可完成该功能,详细如下代码定义:
* */
internal function onUpLoad():void
{
if(fileRef.size > 0)
{
stateText = "正在上传文件:" + fileRef.name;
}
var request:URLRequest = new URLRequest();
//request.url="http://localhost/cdblc/UpLoadHandler.ashx";
request.url="http://localhost:13989/PearWebGis/UpLoadHandler.ashx"
fileRef.upload(request);
Alert.show(request.url.toString());
}
]]>
</fx:Script>
<s:Panel x="61" y="42" width="388" height="225" title="银行资金池信息导入">
<mx:TextInput x="10" y="57" id="txtFile" text="{stateText}" width="229"/>
<mx:Button x="247" y="57" label="选择" fontWeight="normal" click="initApp()"/>
<mx:Button x="29" y="111" label="上传文件" width="111" fontWeight="normal" click="onUpLoad()"/>
<s:Form>
<s:FormItem label="日期:">
<mx:DateField id="dt" dayNames="['日','一','二','三','四','五','六']"
editable="false" formatString="YYYY-MM-DD"
monthNames="['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']"
selectedDate="{new Date()}" yearNavigationEnabled="true"/>
</s:FormItem>
</s:Form>
</s:Panel>
</s:Application>
C#页面
<%@ WebHandler Language="C#" Class="UpLoadHandler" %>
using System;
using System.Web;
using System.IO;
public class UpLoadHandler : IHttpHandler {
//文件上传目录
private string uploadFolder = "UpLoad";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
// mbox("file");
//MessageBox.Show("第一个是消息", "第二个是标题");
HttpFileCollection files = context.Request.Files;
if (files.Count > 0)
{
context.Response.Write("文件长度:" + files.Count);
string path = context.Server.MapPath(uploadFolder);
// mbox(path);
HttpPostedFile file = files[0];
if (file != null && file.ContentLength > 0)
{
string savePath = path + "/" + context.Request.Form["fileName"];
file.SaveAs(savePath); //保存文件
//读取文件内容
int FileLen = file.ContentLength;
byte[] input = new byte[FileLen];
System.IO.Stream UpLoadStream = file.InputStream;
UpLoadStream.Read(input, 0, FileLen);
UpLoadStream.Position = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(UpLoadStream, System.Text.Encoding.Default);
string str = sr.ReadLine();
while (str != null)
{
str = sr.ReadLine();
}
sr.Close();
UpLoadStream.Close();
UpLoadStream = null;
sr = null;
}
}
else
{
context.Response.Write("参数错误");
context.Response.End();
}
}
public bool IsReusable {
get {
return false;
}
}
}
研究了好久不能上传一个参数来设置保存文件的相对位置,没办法就用session来做了