日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

flex上传文件代码

發(fā)布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 flex上传文件代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

帶頁面返回值處理的
<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)+" 字節(jié))?",??
????"確認上傳",??
????Alert.YES|Alert.NO,??
????null,??
????proceedWithUpload);??
??}??
??
??private function onProgress(e: ProgressEvent): void{??
???lbProgress.text = " 已上傳 " + e.bytesLoaded???
????+ " 字節(jié),共 " + e.bytesTotal + " 字節(jié)";??
???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;
??????????? }
??????? }
??? }

總結(jié)

以上是生活随笔為你收集整理的flex上传文件代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。