最近在项目中需要使用到将图片等文件上传到服务器,所以采用Flex+Asp.Net方式进行上传。比较简单,直接贴出源代码:
Flex端:
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
[Bindable]
private var stateText:String="文件名:";
[Bindable]
private var isShow:Boolean=false;
private var isSelect:Boolean=false;
private var file:FileReference = new FileReference();
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
file.addEventListener(Event.SELECT, file_select);
file.addEventListener(Event.COMPLETE, file_complete);
file.addEventListener(ProgressEvent.PROGRESS, file_progress);
}
private function file_select (e:Event):void {
stateText = "文件名: " + file.name;
isSelect=true;
isShow=true;
}
private function file_complete (e:Event):void {
stateText = "上传完毕";
PopUpManager.removePopUp(this);
}
private function file_progress (e:ProgressEvent):void {
stateText = "已上传 " + Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%";
}
private function upload ():void {
if(isSelect==false){
stateText="请先选择文件";
return;
}
if (file.size > 0) {
stateText = "正在上传 " + file.name;
var request:URLRequest = new URLRequest("../PyoWork.aspx");
file.upload(request);
}
}asp.net:
string uploadFolder = "Images/FileImage/Images";
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection files = Request.Files;
if (files.Count == 0)
{
Response.End();
}
string path = Server.MapPath(uploadFolder);
HttpPostedFile file = files[0];
if (file != null && file.ContentLength > 0)
{
string savePath = path + "/" + Request.Form["fileName"];
file.SaveAs(savePath);
}
}当然,这个不是上传图片的最佳方式,因为里面有很多东西我们无法获取,比如上传图片的路径,Flex不能给你显示出路径,只能给你上传的文件名,所以在预览上有麻烦。
彭亚欧个人博客原创文章,转载请注明出处
文章关键词:asp.net 图片上传,flex图片上传
文章固定链接:https://www.pengyaou.com/homeart/MTA=.html
上一篇 C#动态生成RSS数据