A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [RESOLVED] [v7] Upload to server via Flash

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    17

    resolved [RESOLVED] [v7] Upload to server via Flash

    Hi Everyone,

    This thread is linked to this one : MP3 player via FTP
    My main issue is that I would like some students to upload MP3 files an a dedicated server in this way :

    1. They can upload MP3 (without password if possible) from a flash website (made with koolmoves v7)
    2. They can not erase files on the server

    If someone can show me the way

    Thanks in advance

  2. #2
    Junior Member
    Join Date
    Feb 2009
    Posts
    17
    I think it is not an easy task to do in flash but it can be done with embedded Html code inside flash.

    I have this link, it seems to be what I am looking for :

    http://www.cs.tut.fi/~jkorpela/forms/file.html

    If anyone has another idea to achieve this...

  3. #3
    That web bloke Stoke Laurie's Avatar
    Join Date
    Jan 2006
    Location
    England
    Posts
    869
    There is a file upload contribution in www.Koolexchange.com it uses AS1 and was built with V6, but don't let that stop you taking a look, it works very well.

  4. #4
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Working on example, just been busy.
    If you cannot wait look at fileReference on Adobe's site for example.

  5. #5
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Here is example you can try..

    It's working but for some reason I'm not seeing the upload complete. It did once but not now, perhaps someone can figure out what's wrong with it.

    two textfield's one call txt2 the other called stat
    two pushbuttons called pb1 and pb2

    PHP Code:
        import flash.display.Sprite;
        
    import flash.events.*;
        
    import flash.net.FileReference;
        
    import flash.net.URLRequest;
        
    import flash.net.FileFilter;



    myfile= new FileReference()
    mp3Filter = new FileFilter("mp3""*.mp3");
    myfile.addEventListener(ProgressEvent.PROGRESS,fstatus);
    myfile.addEventListener(Event.COMPLETE,fileDone);
    myfile.addEventListener(IOErrorEvent.IO_ERRORioErrorHandler);
    myfile.addEventListener(Event.SELECT,selectHandler);

    function 
    selectHandler(e:Event){
        
    txt2.text=e.target.name;
        }
        
    function 
    fstatus(e:Event){
        
    stat.text="status:"+e.bytesLoaded+" of "+e.bytesTotal;
        }
    function 
    fileDone(e:Event){
        
    stat.text="file uploaded "+e.data;
        }
    function 
    ioErrorHandler(e:Event){
        
    stat.text="IO ERROR "+e;
        }

    pb1.addEventListener(MouseEvent.CLICK,bFile);
    pb2.addEventListener(MouseEvent.CLICK,sendFile);

    function 
    bFile(e:MouseEvent){
        
    myfile.browse([mp3Filter]);
        }
    function 
    sendFile(e:MouseEvent){
        
    stat.text="Trying to send";
        
    uploadURL = new URLRequest();
        
    uploadURL.url="upload.php";
        
    myfile.upload(uploadURL);
        
        } 
    and the PHP code to go on your server (this is a simplfied version with no error checking)

    PHP Code:
    <?php

    $fileName 
    basename($_FILES["Filedata"]["name"]);
    $ok=move_uploaded_file($_FILES["Filedata"]["tmp_name"], $fileName);
    echo 
    $ok;
    ?>

  6. #6
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    Quote Originally Posted by blanius
    $fileName = basename($_FILES["Filedata"]["name"]);
    Quote Originally Posted by blanius
    stat.text="Trying to send";
    uploadURL = new URLRequest();
    uploadURL.url="upload.php";
    myfile.upload(uploadURL);

    Your PHP expects the filename (["name"]), how are you POSTing that above?

  7. #7
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    It gets that fine, that is an named array element of the Array Filedata.
    It's uploading the file fine, including the name being set, I'm just not getting a COMPLETE event.

  8. #8
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    Fixed

    Ok here we go.... Despite the what the docs imply you need to use

    myfile.addEventListener(DataEvent.UPLOAD_COMPLETE_ DATA,completeHandler);

    This not only worked it also gets server response.

    So here is corrected code with attached example fun file.

    PHP Code:
    import flash.display.Sprite;
        
    import flash.events.*;
        
    import flash.net.FileReference;
        
    import flash.net.URLRequest;
        
    import flash.net.FileFilter;



    myfile= new FileReference();
    mp3Filter = new FileFilter("mp3""*.mp3");
    myfile.addEventListener(ProgressEvent.PROGRESS,fstatus);
    myfile.addEventListener(IOErrorEvent.IO_ERRORioErrorHandler);
    myfile.addEventListener(Event.SELECT,selectHandler);
    myfile.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeHandler);

    function 
    completeHandler(e:Event){
        
    stat.text=e.data;
        }
    function 
    selectHandler(e:Event){
        
    txt2.text=e.target.name;
        }
        
    function 
    fstatus(e:Event){
        
    stat.text="status:"+e.bytesLoaded+" of "+e.bytesTotal;
        }

    function 
    ioErrorHandler(e:Event){
        
    stat.text="IO ERROR "+e;
        }

    pb1.addEventListener(MouseEvent.CLICK,bFile);
    pb2.addEventListener(MouseEvent.CLICK,sendFile);

    function 
    bFile(e:MouseEvent){
        
    myfile.browse([mp3Filter]);
        }
    function 
    sendFile(e:MouseEvent){
        
    stat.text="Trying to send";
        
    uploadURL = new URLRequest();
        
    uploadURL.url="upload.php";
        
    myfile.upload(uploadURL);
        
        } 
    PHP Code:
    <?php

    $fileName 
    basename($_FILES["Filedata"]["name"]);
    $ok=move_uploaded_file($_FILES["Filedata"]["tmp_name"], $fileName);
    if (
    $ok)
    {
        
    $message =  "$fileName uploaded successfully.";
    }
    else 
    {
        
    $message "Somthing is wrong with uploading a file.";
    }
     
    echo 
    $message;

    ?>
    Attached Files Attached Files

  9. #9
    Junior Member
    Join Date
    Feb 2009
    Posts
    17
    I have no time for the moment to test your code I would like to thank very much for the help.

    I finish the integration of the mp3 player and then I will tell you how it is working for me.
    Very nice forum

  10. #10
    Junior Member
    Join Date
    Feb 2009
    Posts
    17
    Thanks a lot ... it works very nicely

    But one question how to set a different target on my server, I don't want the file to go on the root directory but in the directory called "mixages fous".

    And an other one, I want people who want to upload to leave me some informations like : first name, last name, phone number, email, working location. If it could be written in a txt file on the server it would be nice.
    I don't know if you can help me on this aspect but you help very much on the upload side

    Thanks again

  11. #11
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Change this line
    $ok=move_uploaded_file($_FILES["Filedata"]["tmp_name"], "../mixages fous/".$fileName);

  12. #12
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    To post data along with the upload, you need to create some sort of form.
    On the php backend you can place it in a text file, mail it to yourself or place it in a database if you have one.

    Another thing you might want to consider is users uploading content with the same filename resulting in problems. To prevent that, you could add a timestamp to the filename and change the routine that creates the xml file so that it leaves the timstamp in for the url to access but removes it for the title that is being shown.

  13. #13
    Junior Member
    Join Date
    Feb 2009
    Posts
    17
    I have modified the php file and now I upload in the good directory... thanks

    About the form to fullfil could you give me one last help on this.
    The best feature would be to rename the file uploaded like firstname_lastname.mp3 automatically.
    If there are two times the same entries it will overwrite the file on the server.
    The form will increase a txt file on the server.
    I would like those informations to be written in order to upload :
    first name, last name, phone number, email, working location

    If you have no time to develop just give me the procedure.

    Thanks to your help I have created most of what I needed, I am very grateful

    You can see the website still in dev here : http://www.mixagefou.com/

  14. #14
    Junior Member
    Join Date
    Feb 2009
    Posts
    17
    I have found some very usefull help on forms on the koolexchange website : http://www.koolexchange.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center