flex上传文件代码
帶頁面返回值處理的
<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"
????creationComplete="init()" xmlns:local="*">
?
?<fx:Script>
??<![CDATA[
???
??private const defaultRequestUrl : String = "http://192.168.0.212:8002/Talk/UploadHandler.ashx";
??
??private var file : FileReference;
??
??private function init():void {
???Security.allowDomain("*");
???
???file = new FileReference();
???file.addEventListener(Event.SELECT, onFileSelect);
???file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
???file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, completeHandle);
???//file.addEventListener(Event.OPEN, openHandle);
???//file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
???//file.addEventListener(Event.CANCEL, cancelHandler);
??}
??
??private function onClickBrowserBtn() : void {
???file.browse(getTypeFilter());
??}
??
??private function getTypeFilter() : Array {
???var imagesFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
???//var docFilter:FileFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");
???
???return [imagesFilter];
??}
??
??private function onFileSelect(event : Event) : void {
???uploadBtn.enabled = true;
???infoText.htmlText =
?????"Name: " + file.name + "<br/>" +
?????"Size: " + file.size + "<br/>" +
?????"Type: " + file.type + "<br/>" +
?????"Date: " + file.creationDate;
??}
??
??private function onClickUploadBtn() : void {
???var request : URLRequest = new URLRequest(defaultRequestUrl);
???request.data = "userId=123";
???file.upload(request);
??}
??
??private function progressHandle(event : ProgressEvent) : void {
???progressLabel.text = "complete " + event.bytesLoaded + " bytes";
???var fileUploadPercent : uint = event.bytesLoaded / event.bytesTotal * 100;
???uploadProgressBar.setProgress(fileUploadPercent, 100);
???uploadProgressBar.label = "Complete " + fileUploadPercent + "%";
??}
??
??private function completeHandle(event : DataEvent) : void {
???infoText.htmlText = "Upload " + file.name + " Complete!<br>"+event.data;
???uploadBtn.enabled = false;
??}
???
??]]>
?</fx:Script>
?
?<mx:Button id="browserBtn" x="10" y="69" label="Browser"
????? click="onClickBrowserBtn()"/>
?
?<mx:Button id="uploadBtn" x="236" y="69" label="Upload" enabled="false"
????? click="onClickUploadBtn()"/>
?
?<mx:ProgressBar id="uploadProgressBar" x="10" y="33" width="291"
?????maximum="100" direction="right" mode="manual"/>
?
?<mx:TextArea id="infoText" x="10" y="99" width="291" height="131"/>
?<mx:Label id="progressLabel" x="10" y="10" width="291"/>
</s:Application>
?
?
示例1:
<?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" xmlns="*" creationComplete="init();">??
?<fx:Script>??
??<![CDATA[?
??import flash.net.FileReference;??
??import mx.controls.Alert;??
??import mx.events.CloseEvent;??
??import flash.events.*;??
??
??private var file: FileReference;??
??
??private function init(): void{??
???Security.allowDomain("*");??
???file = new FileReference();??
???file.addEventListener(ProgressEvent.PROGRESS, onProgress);??
???file.addEventListener(Event.SELECT, onSelect);??
??}??
??
??private function upload(): void{??
???file.browse();??
??}??
??
??private function onSelect(e: Event): void{??
???Alert.show("上傳 " + file.name + " (共 "+Math.round(file.size)+" 字節)?",??
????"確認上傳",??
????Alert.YES|Alert.NO,??
????null,??
????proceedWithUpload);??
??}??
??
??private function onProgress(e: ProgressEvent): void{??
???lbProgress.text = " 已上傳 " + e.bytesLoaded???
????+ " 字節,共 " + e.bytesTotal + " 字節";??
???var proc: uint = e.bytesLoaded / e.bytesTotal * 100;??
???bar.setProgress(proc, 100);??
???bar.label= "當前進度: " + " " + proc + "%";??
??}??
??
??private function proceedWithUpload(e: CloseEvent): void{??
???if (e.detail == Alert.YES){??
????var request: URLRequest = new URLRequest("http://192.168.0.212:8002/Talk/UploadHandler.ashx");??
????try {??
?????file.upload(request);??
????} catch (error:Error) {??
?????trace("上傳失敗");??
????}??
????
???}??
??}??
?? ]]>??
??</fx:Script>??
????
??<mx:Canvas width="100%" height="100%">??
???<mx:VBox width="100%" horizontalAlign="center">??
????<mx:Label id="lbProgress" text="上傳"/>??
????<mx:ProgressBar id="bar" labelPlacement="bottom"
????????minimum="0" visible="true" maximum="100" label="當前進度: 0%"?????
????????direction="right" mode="manual" width="200"/>??
????<mx:Button label="上傳文件" click="upload();"/>???????????????
???</mx:VBox>??
??</mx:Canvas>??
</s:Application>??
示例2:
<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"
????creationComplete="init()">
?
?<fx:Script>
??<![CDATA[
???
???private const defaultRequestUrl : String = "http://192.168.0.212:8002/Talk/UploadHandler.ashx";
???
???private var file : FileReference;
??
??private function init():void {
???Security.allowDomain("*");
???
???file = new FileReference();
???file.addEventListener(Event.SELECT, onFileSelect);
???file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
???file.addEventListener(Event.COMPLETE, completeHandle);
???//file.addEventListener(Event.OPEN, openHandle);
???//file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
???//file.addEventListener(Event.CANCEL, cancelHandler);
??}
??
??private function onClickBrowserBtn() : void {
???file.browse(getTypeFilter());
??}
??
??private function getTypeFilter() : Array {
???var imagesFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
???//var docFilter:FileFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");
???
???return [imagesFilter];
??}
??
??private function onFileSelect(event : Event) : void {
???uploadBtn.enabled = true;
???infoText.htmlText =
?????"Name: " + file.name + "<br/>" +
?????"Size: " + file.size + "<br/>" +
?????"Type: " + file.type + "<br/>" +
?????"Date: " + file.creationDate;
??}
??
??private function onClickUploadBtn() : void {
???var request : URLRequest = new URLRequest(defaultRequestUrl);
???request.data = "userId=123";
???file.upload(request);
??}
??
??private function progressHandle(event : ProgressEvent) : void {
???progressLabel.text = "complete " + event.bytesLoaded + " bytes";
???var fileUploadPercent : uint = event.bytesLoaded / event.bytesTotal * 100;
???uploadProgressBar.setProgress(fileUploadPercent, 100);
???uploadProgressBar.label = "Complete " + fileUploadPercent + "%";
??}
??
??private function completeHandle(event : Event) : void {
???infoText.htmlText = "Upload " + file.name + " Complete!";
???uploadBtn.enabled = false;
??}
??]]>
?</fx:Script>
?
?<mx:Button id="browserBtn" x="10" y="69" label="Browser"
????? click="onClickBrowserBtn()"/>
?
?<mx:Button id="uploadBtn" x="236" y="69" label="Upload" enabled="false"
????? click="onClickUploadBtn()"/>
?
?<mx:ProgressBar id="uploadProgressBar" x="10" y="33" width="291"
?????maximum="100" direction="right" mode="manual"/>
?
?<mx:TextArea id="infoText" x="10" y="99" width="291" height="131"/>
?<mx:Label id="progressLabel" x="10" y="10" width="291"/>
?
</s:Application>
.net后臺
/// <summary>
??? /// UploadHandler 的摘要說明
??? /// </summary>
??? public class UploadHandler : IHttpHandler
??? {
??????? public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.ContentType = "text/plain";
??????????? context.Response.Charset = "utf-8";
??????????? HttpPostedFile file = context.Request.Files["Filedata"];
??????????? if (file != null)
??????????? {
??????????????? string newFilePath = context.Server.MapPath("~/") + "/files/" + DateTime.Now.ToString("yyyy") + "\\" + DateTime.Now.ToString("MM") + "\\" + DateTime.Now.ToString("dd") + "\\";
??????????????? Directory.CreateDirectory(newFilePath);
??????????????? string newFileName = DateTime.Now.Ticks.ToString() + file.FileName.Substring(file.FileName.LastIndexOf('.'));
??????????????? file.SaveAs(newFilePath + newFileName);
??????????????? string url = "/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/" + newFileName;
??????????????? //context.Session["MovieFile"] = new PFileInfo(file.FileName, file.ContentLength, url, file.FileName.Substring(file.FileName.LastIndexOf('.')));
??????????????? context.Response.Write(url);
??????????? }
??????????? else
??????????? {
??????????????? context.Response.Write("");
??????????? }
??????? }
??????? public bool IsReusable
??????? {
??????????? get
??????????? {
??????????????? return false;
??????????? }
??????? }
??? }
總結
以上是生活随笔為你收集整理的flex上传文件代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 列表和range、元组
- 下一篇: ISO14443 PICC 与 PCD