4 Replies Latest reply on Dec 5, 2007 2:59 AM by OptimusFlex

    how to upload a file with flex application?

    mark416 Level 1
      Hi,

      I need build a file upload function with Flex and Coldfusion.but I do not know which build-in component can do this.

      Please give me a idea about this.


      Thanks for help

      M
        • 3. Re: how to upload a file with flex application?
          OptimusFlex
          Hello How should understand this trace when uploading file to php application:

          what does eventPase = 2 means? same thing for bubbles=false?
          [CODE]
          progressHandler: name=4-nov-2007.gpx bytesLoaded=32768 bytesTotal=169869
          progressHandler: name=4-nov-2007.gpx bytesLoaded=65536 bytesTotal=169869
          progressHandler: name=4-nov-2007.gpx bytesLoaded=98304 bytesTotal=169869
          progressHandler: name=4-nov-2007.gpx bytesLoaded=131072 bytesTotal=169869
          progressHandler: name=4-nov-2007.gpx bytesLoaded=163840 bytesTotal=169869
          progressHandler: name=4-nov-2007.gpx bytesLoaded=169869 bytesTotal=169869
          completeHandler: [Event type="complete" bubbles=false cancelable=false eventPhase=2]
          [DataEvent type="uploadCompleteData" bubbles=false cancelable=false eventPhase=2 data="0"][/CODE]

          • 4. Re: how to upload a file with flex application?
            OptimusFlex Level 1
            first code is flex, the second one is php, i would ike to know if they do the same thing: upload a file to http://server.com/plateform/ul/index.php?


            [CODE]<?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400" height="300"
            initialize="init()"
            >

            <mx:Script>
            <![CDATA[
            import com.project.community.model.CommunityModel;
            import com.project.community.model.EditorModel;
            import mx.controls.Alert;
            import mx.controls.ProgressBar;
            import mx.utils.ObjectUtil;
            import flash.events.*;
            import flash.net.FileReference;
            import flash.net.URLRequest;

            /**
            *
            */
            private const UPLOAD_URL : String = " http://server.com/plateform/ul/index.php";


            private var _progressBar: ProgressBar = new ProgressBar();
            private var _fileRef : FileReference = new FileReference();
            private var _gpsTypes : FileFilter = new FileFilter("Text Files (*.gpx)", "*.gpx");




            public function init() : void
            {
            initListener();
            }



            public function initListener() : void
            {
            submit.addEventListener(MouseEvent.CLICK, submitClickHandler);
            back.addEventListener(MouseEvent.CLICK, backClickHandler);

            _fileRef.addEventListener(Event.SELECT, fileSelectedHandler);
            _fileRef.addEventListener(ProgressEvent.PROGRESS, progressEventHandler);

            _fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            _fileRef.addEventListener(Event.COMPLETE, uploadCompletedHandler);
            _fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            _fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, serverResponseHandler);
            }


            public function httpStatusHandler(event:HTTPStatusEvent): void
            {
            trace("httpStatusHandler: " + event);
            }



            /*public function httpStatusHandler(event:HTTPStatusEvent):void
            {

            if (event.status != 200)
            {
            if (event.status == 500){
            mx.controls.Alert.show("Probleme d'écriture","Error");
            return;
            }
            if (event.status == 404){
            mx.controls.Alert.show("Fichier non trouvé","Error");
            return;
            }
            if (event.status == 415){
            mx.controls.Alert.show("Type de media interdit","Error");
            return;
            }
            if (event.status == 401){
            mx.controls.Alert.show("Acces refusé","Error");
            return;
            }
            mx.controls.Alert.show("Erreur "+event.status,"Error");
            }
            }
            */




            /**
            * function that handles the event: click on browse button
            */
            public function fileSelectedHandler(event : Event) : void
            {

            selectedfile.text = _fileRef.name;
            progressBar.setProgress(0, 100);
            //progressBar.label = "Loading 0%";
            enabled = true;
            //trace(selectedfile.text);
            }



            /**
            * function that handle the event: click on back button.
            */
            public function submitClickHandler(event : MouseEvent) : void
            {
            var params : URLVariables = new URLVariables();
            var uploadRequest : URLRequest = new URLRequest(UPLOAD_URL);
            params.filename = _fileRef.name;
            params.sk = CommunityModel.getInstance().serverSessionKey; // "9b166dc3b1a6bf702dfd87b825627b6a";
            params.dk = "7ffcd213ac5e00a88a2968c6f3530aeb";
            params.type = "track";

            //affichage de l'erreur exact retournée par Icelio
            //params.view_error = true;


            uploadRequest.method = URLRequestMethod.POST;
            uploadRequest.data = params;
            trace(uploadRequest.url);
            trace(uploadRequest.data);
            trace(params.filename);
            _fileRef.upload(uploadRequest);


            // progressBar.label = "Uploading...";

            // EditorModel.getInstance().changeEditor(EditorModel.C_NAVIGATOR_NUMBER);
            }

            public function progressEventHandler(event : ProgressEvent): void
            {
            var file:FileReference = FileReference(event.target);
            trace("progressHandler: name=" + _fileRef.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
            _progressBar.setProgress(event.bytesLoaded, event.bytesTotal);

            }

            public function uploadCompletedHandler(event : Event):void
            {
            trace("completeHandler: " + event);

            }



            public function serverResponseHandler( event:DataEvent ) :void
            {
            trace(event);
            var response : XML = XML( event.data );
            trace(response);

            }

            /**
            * function that handle the event: click on back button
            */
            public function backClickHandler(e : MouseEvent) : void
            {
            EditorModel.getInstance().changeEditor(EditorModel.C_NAVIGATOR_NUMBER);
            }




            public function ioErrorHandler (event : IOErrorEvent) : void
            {
            trace("ioErrorHandler: " + event);
            }

            ]]>
            </mx:Script>

            <mx:Panel title="{EditorModel.getInstance().langTraceInserterterTitle}" top="0" bottom="15" left="1" right="10">

            <mx:Canvas verticalScrollPolicy="auto" width="100%" height="100%">


            <mx:TextInput x="10" y="20" id="selectedfile" />
            <mx:Button x="167" y="20" id="browse" label="{EditorModel.getInstance().langTraceInserterterBrowser}"
            click="_fileRef.browse([_gpsTypes])" />


            <!-- progress bar -->

            <mx:ProgressBar label="Transfert Progress" id="progressBar" mode="manual" x="10" y="50"/>



            <!-- submit -->
            <mx:Button id="submit" label="{EditorModel.getInstance().langTraceInserterterValidate}"
            buttonMode="true" useHandCursor="true" width="120"
            styleName="submit" right="10" top="10"
            />
            <mx:Button id="back" label="{EditorModel.getInstance().langTraceInserterterCancel}"
            buttonMode="true" useHandCursor="true" width="120"
            styleName="cancel" y="65" right="10"
            />

            </mx:Canvas>

            </mx:Panel>
            </mx:Canvas>[/CODE]



            [CODE]<?php

            define('FOLDER_ROOT', '../..');
            $load_param = array('session','template','cnty_tools_main','cnty_tools_action','cnty_conf','error','xss ');
            require_once('head.php');

            include PATH_TEMPLATES_CONFIG."/_config_account.php";
            $aTemplates['Account_Form'] = file_get_contents($cfg['path_tmpl_list'].'tmpl_trace_new.html');

            function DoOutput ()
            {
            global $rt;
            echo TemplateProcess($rt['output']);
            }
            $addr = $cfg['this_url'];

            //$sub_name = substr($_SESSION['sess_key_plateform'], 0, 10);
            /* Send file
            @param string $url Url
            @param array $file File array
            @param array $var Var array
            @param string $boundary Separator
            @return array with header and content client response
            */
            function header_build($url, $file, $var = null, $boundary = '---------------------------9633331271588363780939124723') {

            $response = null;

            $tab = parse_url($url);
            echo ($tab);
            $fp = fsockopen($tab['host'], 80, $errno, $errstr, 30);

            $parsingfile = fopen('parsingfile.txt', 'r+');
            fseek($parsingfile, 0);
            fputs($parsingfile, $tab);
            fclose($parsingfile);

            //$parsingresult = file_get_contents($parsingfile);
            echo ($parsingresult);


            if ($fp) {

            // Build the header
            $header = "POST ".$tab['path']." HTTP/1.0\r\n";
            $header .= "Host: ".$tab['host']."\r\n";
            $header .= "Content-type: multipart/form-data, boundary=$boundary\r\n";

            $data = null;

            // Add post var
            foreach ($var as $k => $v) {
            $data .="--$boundary\r\n";
            $data .= "Content-Disposition: form-data; name=\"".$k."\"\r\n";
            $data .= "\r\n".$v."\r\n";
            }

            // Attach files
            foreach ($file as $f) {
            $data .= "--$boundary\r\n";
            $data .="Content-Disposition: form-data; name=\"ul_files[]\"; filename=\"".basename($f)."\"\r\n";
            $data .= "Content-Type: unknow/unknow\r\n\r\n";
            $data .= file_get_contents($f)."\r\n";
            $data .= "--$boundary\r\n";
            }

            $header .= "Content-length: ".strlen($data)."\r\n\r\n";

            // Write !
            fwrite($fp, $header.$data);

            $data = null;

            // Read response
            while (!feof($fp)) {
            $data .= fread($fp, 32);
            }

            ?>
            [/CODE]