zoukankan      html  css  js  c++  java
  • Flex+PHP实现上传图片|Flex+PHP Realization picture uploading

    1 Flex代码

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init();" > 
        <mx:Script>          <![CDATA[ 
                import flash.net.FileReference; 
                import mx.controls.Alert; 
                import mx.events.CloseEvent; 
                import flash.events.*; 
                private var file : FileReference; 
                private var uploadURL : URLRequest; 
                private function init() : void{ 
                    Security.allowDomain("*");  
                    file = new FileReference();  
                    file.addEventListener(ProgressEvent.PROGRESS, onProgress);  
                    file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUPLOAD_COMPLETE_DATA);
                    file.addEventListener(Event.SELECT, onSelect); 
                    uploadURL = new URLRequest();  
                    uploadURL.url = "http://localhost:80/ShowProduct-debug/uploadFile.php";
                    uploadURL.method = "post"; 
                } 
                private function upload() : void{ 
                    var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png"); 
                    var allTypes:Array = new Array(imageTypes); 
                    file.browse(allTypes); 
                } 
                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 + " 字节"; 
                } 
                private function proceedWithUpload(e : CloseEvent) : void{ 
                    if (e.detail == Alert.YES){ 
                        file.upload(uploadURL); 
                    } 
                }
                private function onUPLOAD_COMPLETE_DATA(evt:DataEvent):void
                {
                 Alert.show(evt.data.toString());
                } 
            ]]> 
        </mx:Script> 
        <mx:Canvas width="100%" height="100%"> 
            <mx:VBox width="100%" horizontalAlign="center"> 
            </mx:VBox> 
            <mx:Button label="上传文件" click="upload();" y="91"/> 
            <mx:Label id="lbProgress" text="上传" x="87.5" y="26"/> 
        </mx:Canvas> 
    </mx:Application>

    注:uploadURL.url = "http://localhost:80/ShowProduct-debug/uploadFile.php";路径一定要写对,否则回报Error #2044: 未处理的 IOErrorEvent:。 text=Error #2038: 文件 I/O 错误。
    2. php代码

    <?php   
    // Flash 传递的文件表单 name 属性为 Filedata   
    $fileName = $_FILES["Filedata"]["name"]; 
    $file = $_FILES["Filedata"]["tmp_name"];   
    $path = "uploadFiles/";
    if(!is_dir("./uploadFiles")){
      @mkdir("./uploadFiles");
    }  
    if (move_uploaded_file($file, $path . $fileName)){   
      echo Success;   
    }else{   
      echo Fail;   

    ?>

    注:php文件一定要放到Apache运行的目录里,例如:把php文件放到C:\xampp\htdocs\ShowProduct-debug目录下

    --------------------- 我可以操纵冰冷的代码,但我操纵不了我的人生...... [url=http://www.puya360.com]西安普雅软件工作室[/url]
  • 相关阅读:
    C#获取当前路径的七种方法
    map容器对象插入数据的4种方式【转】
    为VMware虚拟机内安装的Ubuntu 16.04设置静态IP地址【转】
    Ubuntu16手动配置IP地址
    当前上下文不存在名称“***”
    C++读写文件ofstream,ifstream,fstream)[转]
    atoi()函数
    C++常用输出 cout、cerr、clog
    Lucene教程(转)
    selenium环境搭建1
  • 原文地址:https://www.cnblogs.com/ternastone/p/1855494.html
Copyright © 2011-2022 走看看