Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/1/30 1:01:53
Flex+asp.net上传文件_[Asp.Net教程]

Flex+asp.net上传文件_[Asp.Net教程]

前台Flex文件:UploadSample.mxml,其代码如下所示:

1
2
3
4 global
5 {
6 fontSize : 12;
7 }
8

9
10
11 12 // 先搞 1 个 FileReference
13 private var file:FileReference = new FileReference();
14
15 // 上传状态指示, 和下面的文本框绑定
16 [Bindable]
17 private var stateText:String = "请选择一个文件上传";
18
19 // createChildren 比 creationComplete 事件更早发生, 省的注册事件侦听, 直接在这里写了
20 protected override function createChildren():void
21 {
22 super.createChildren();
23 file.addEventListener(Event.SELECT, file_select);
24 //file.addEventListener(Event.COMPLETE, file_complete);
25 file.addEventListener(ProgressEvent.PROGRESS, file_progress);
26 file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, file_complete);
27 }
28
29 // 选择 1 个文件的事件
30 private function file_select (e:Event):void
31 {
32 stateText = "选择了文件 " + file.name;
33 }
34
35 // 上传完毕后的事件
36 private function file_complete (e:Event):void
37 {
38 stateText = "上传完毕";
39 }
40
41 private function file_progress (e:ProgressEvent):void
42 {
43 stateText = "已上传 " + Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%";
44 }
45 // 先判断一下文件大小, 再上传, FileService.aspx 就是上传地址
46 private function upload ():void
47 {
48 if (file.size > 0)
49 {
50 stateText = "正在上传 " + file.name;
51 var request:URLRequest =
52 new URLRequest("http://localhost:1851/WebSite1/FileService.aspx");
53 file.upload(request);
54 }
55 }
56
57
58 ]]>
59

60
61 62 verticalAlign="middle" horizontalAlign="center" >
63
64
65
66

67
68
69

70

71

72
服务器端asp.net文件:FileService.aspx.cs,其代码如下所示:

1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class FileService : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 string uploadFolder = "upload"; // 上传文件夹
17
18 HttpFileCollection files = Request.Files;
19
20 if (files.Count == 0)
21 {
22 Response.Write("请勿直接访问本文件");
23 Response.End();
24 }
25
26 string path = Server.MapPath(uploadFolder);
27
28 // 只取第 1 个文件
29 HttpPostedFile file = files[0];
30
31 if (file != null && file.ContentLength > 0)
32 {
33 // flash 会自动发送文件名到 Request.Form["fileName"]
34 string savePath = path + "/" + Request.Form["fileName"];
35 file.SaveAs(savePath);
36 }
37 }
38 }
说明:
客户端Flex使用URLRequest对象请求一个服务器端的连接,在服务器端使用HttpFileCollection对象接收客户端的请求。flash 会自动发送文件名到 Request.Form["fileName"]。
效果如下所示:


来源:http://www.cnitblog.com/Lalo





If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759