Flex3上传附件至服务器

private var file:FileReference;

private var displayTypes:FileFilter=new FileFilter("Excel文件(*.xls)","*.xls");
private var fileFilter:Array=new Array(displayTypes);

//初始化

file = new FileReference();
file.addEventListener(Event.SELECT, onSelect);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,xmlComplete);

//浏览本地文件

file.browse(fileFilter);

 private function onSelect(e: Event): void
{
            Alert.show("上传 " + file.name + " (共 "+Math.round(file.size)+" 字节)?",
                            "确认上传",Alert.YES|Alert.NO,null,proceedWithUpload);                        

}

 private function proceedWithUpload(e:CloseEvent): void
 {
             if (e.detail == Alert.YES)
             {
              var upLoadUrl:String=spExcelUrl;
                var request:URLRequest = new URLRequest(upLoadUrl);
                CursorManager.setBusyCursor();
                file.upload(request);
               
                //loading
               swfLoader.source="assets/2/loading.swf";
               swfLoader.alpha=0.8;
               swfLoader.isPopUp=true;
               mx.managers.PopUpManager.addPopUp(swfLoader, DisplayObject(parentApplication), true);
               mx.managers.PopUpManager.centerPopUp(swfLoader); 
             }
 }

 

    //c#

    string uploadFolder = "upload"; // 上传文件夹
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpFileCollection files = Request.Files;

        if (files.Count == 0)
        {
            Response.Write("请勿直接访问本文件");
            Response.End();
        }

        string path = Server.MapPath(uploadFolder);

        // 只取第 1 个文件
        HttpPostedFile file = files[0];

        if (file != null && file.ContentLength > 0)
        {
            // flash 会自动发送文件名到 Request.Form["fileName"]
            string savePath = path + "/" + Request.Form["fileName"];
            file.SaveAs(savePath);
            getXml(savePath);
            System.IO.File.Delete(savePath);
        }

    }

你可能感兴趣的:(Flex)