FileReference example

The new FileReference object enables you to upload and download files using Flash 8 and a server side script (required for upload only).

Here is an example I put together for the forthcoming book by friends of ED titled ‘Flash 8 Upgrade Essentials’. In the chapter I walk through the ActionScript file and FLA and explain how it all works. The other authors on the book are Matt Voerman, Craig Swann, Glen Rhodes, Todd Yard and Guy Watson.

File Reference example

The book should be out early November so save your pennies!

91 Responses to “FileReference example”

  1. tripleaxis says:

    That is the nuts! That’s so cool.

    oh. and gimme the bloody source.. flash 8 is released now :)

    how does the upload/download progress work?

  2. Tink says:

    FileReference fires events that you can set an object to listen for.

    Its very much like MovieClipLoader.

  3. Mani says:

    Obviously a lot of people are quite happy about this new feature. Is there anyone out there as disappointed as I am about the fact that you can’t select several files at once in the dialog box?

    Okay, now the Flash Player is capable of doing something which before had to be done by the browser, but to tell the truth, it doesn’t help me in varios of my projects because of its lack of ‘multiple selection’.

  4. Adam says:

    Notice that in the Browse box for saving, it fills in the filename without an extension but give the file type as “All Files (*.*)”. Is there any to get it to show that I’m about to download a ZIP file? In the upload, it lets you choose an “Image File” or a “Flash Movie”, but there’s no way of knowing what Image types are acceptable. I guess I’ll have to wait for the source/book to see your implementation, but maybe this is a problem with MM’s component?

  5. Chris Southam says:

    Nice work indeed, pretty neat stuff – except I uploaded one 45byte GIF file and one 5.35Kb GIF file and they both measured 45Kbs abd 535Kb in size – might be worth tackling?

  6. alex says:

    not source until Flash 8 is released???
    thank you.
    very usefull.

  7. alex says:

    Sorry not source until Flash 8 is released?
    thank you

  8. Tink says:

    Mani, you’ll be extremely pleased to know that in the example i have used FileReference, which has the functionality to upload and download a single file. But FileReferenceList has the functionality to upload multiple files.

    Adam i don’t believe it is possible to change what is shown in the ‘Save as type’ field, but you are able to specify the file name of the download.

    ‘Image File’ and ‘Flash Movie’ are descriptions i’ve specified, along with the file types users are allowed to upoad. The user will only see the file types allowed when they browse their OS, but you could display whatever you wanted in the ‘Files of type’ field.

    And sorry alex no source at the mo.

  9. [...] Files Flash 8 File Upload Example Now this is SWEET! Simply put, check it out. This entry was posted on Tuesday, August 16th [...]

  10. William from Lagos says:

    How is upload made using ColdFusion (CFML)?

  11. Ryan says:

    I was just wondering, since this is a flash player feature are developers going to have to upgrade from MX2004 to get the file upload/download components and classes.

    I guess so?

  12. Tink says:

    Wiiliam, i know absolutely nothing about ColdFusion, but here is a simple ColdFusion script for uploading a file.

    Ryan, all end users will need the Flash 8 player installed. I don’t believe you will have to upgrade your IDE as you could create a Flash 8 SWF with open source tools. That said for how cheap the IDE is for what you get, i would definately recommend buying it.

  13. I love the file info that comes up before uplaoding! AWESOME!

  14. Is there any ASP version of this thing around, i really need this for my next project.

  15. Abhs says:

    Wow!! thats great, upload and download will be a great surviover for us

  16. Dave Kurm says:

    Any chance of posting the source now that Flash 8 has been released?

  17. Greg says:

    Well, flash 8 is now released. I’ve tried testing an upload with the new filereference however I get a sandbox restriction and have a policy file in place that allows all domains. Can you release the source now since 8ball is here?

  18. Lar says:

    Flash 8 is available, any chance the FileReference source will be made available soon? Thx.

  19. Stefan says:

    flash 8 is released can we have the code please?

  20. Josh says:

    Very Cool!

    Where can I download a working version with php code instead of coldfusion? Would it be possible for you to post it? Thanks.

  21. Tink says:

    I apologise for the slow response here. Things have been a little hectic recently. I made this example for friends of ED and therefore the source will be available on their site once the book is out.

    I will upload 2 examples tomorrow with the full source, or for now you can view the presentation i did at London MMUG last week at http://www.tink.ws/mmug (use your cursor keys to navigate).

  22. Dan says:

    Well that was a waste of my time. Take the link off unless you intend to put up the source.

  23. Tink says:

    Charming!

    Your saying that developers shouldn’t show any their work or experiments unless they provide the source?

    Anyway, this example was done for a book, which is clearly explained in the comments above, so the source will be free and available, but from the publishers site, once the book is released.

    Because of this i created 2 other examples with source (also expained above) for people who wanted the source asap.

  24. Developer says:

    This is cool, but pointless to the developer as there is no source available. Thanks anyways..

  25. Tink says:

    As metioned above, this example was done for a book, so the source will be free and available, but from the publishers site, once the book is released.

    Until then I added the follwoing examples with source.

    FileReference Source

  26. aspfiend says:

    Anyone find an ASP script for this download component yet?

  27. Tink says:

    I’ve asked a bud of mine who works in ASP and was told you need to use components in ASP to upload files, you can’t just use a bit of script. I’d be interested on someone confirming this as the case.

  28. Prion says:

    You can upload in ASP using pure script. But it’s a mess of code.

  29. 3221314 says:

    I havent got flash 8 yet do I have to pay to get it?

  30. Tink says:

    You can get a 30 day free trial from Adobe, but then you’ll have to pay.

  31. 3221314 says:

    Ok thanks

  32. MarkNL says:

    Thanks Tink, keep it up!
    And don’t let those frustrated geeks like Dan and Developer get to you :D .
    @ 3221314: ASP needs components to upload files (a good one is ASPUpload). They usually cost….
    I’ve you use php it’s free and easy; just one line off code:

  33. MarkNL says:

    i meant ‘if’ (not i’ve) :P

  34. Christian says:

    You do not need a component to upload files via ASP

    I have used http://www.freeaspupload.net/ with no problems, no mesy code just use the include file

  35. Tyro says:

    I set the file type display,then download a file which be renamed.The file’s type can’t be distinguish

  36. [...] que comprobéis vosotros mismos que lo que digo es verdad: http://klr20mg.com/?p=57 http://www.tink.ws/blog/filereference-example/ (en esta, os podréis bajar un ejemplo que, despu [...]

  37. Dok says:

    OK, I see someone has gotten this to work w/ the FreeASPUpload that’s available. Do you have a code snippet to share?

    I freakin’ hate ASP, but I don’t have choice for what I’m doing for this client (nor am I or do I want to be an ASP ‘expert’ either). I can get the FileReference to upload to my script on the server, but that’s about it. The documentation for the FreeASPUpload sux, and the class seems to be hard-coded to use naming that’s inconsistent with the Flash naming in the HTTP request.

    ANY help with this would be greatly appreciated…

  38. Mayur says:

    Good Logic For Upload .
    Pls Send code for this

  39. Tink says:

    Hey Dok, unfortunately I know bugger all about ASP.

    Mayur, all the code is in the book or you can download the example from the friends of ED or or can view and download the files from my presentation ‘A Round Trip with FileReference‘.

  40. The Bagpiper says:

    where can I find the sourse of the upload examle ty …. post it we need it we have to learn because Im not so good whit AS

  41. Tink says:

    With fear of repeating myself…

    Bagpiper, all the code is in the book or you can download the example from the friends of ED or or can view and download the files from my presentation ‘A Round Trip with FileReference‘.

  42. Andrzej says:

    Great work, Tink.. You had posted up a link for a ColdFusion page example, but the link doesn’t lead anywhere anymore. Any chance of putting this up again? I downloaded some of the other examples which are featured in your book – some really interesting techniques indeed…

  43. Andrzej says:

    Ignore my last post, I worked it out… for anyone else you do it like this:

  44. please give us the sorce! :)

  45. Tink says:

    With fear of repeating myself… errr again…

    All the code is in the book, or you can download the example from friends of ED.

  46. Bennr says:

    HEY! STOP!

    This is how you get it:

    go to the friends of ED website by following the link in the post above. Click on Downloads and choose the bottom download, the Appendix one. Unzip and look for fileReferenceExample.

    The source is in there.

  47. LEO says:

    Hi there!
    Nice work! Although I am having some problems and was wondering if you could help me.
    I am using your example to upload a file to my server using flash. But I need to change the files name when is fully uploaded. Lets say, if at the clients side is one.dat I must upload it to my server as two.dat. How could I do this and what modifications on the code must I do? I tried to use rename in a php file but the “rename” must be during the uploading.
    Thanks.
    Cheers.
    Leo.

  48. deevee says:

    yooooo…. developers begging for sourcecodes, then what is you guys developing… the flash help and some brains can get you everywhere… respect to tink

  49. Patrick says:

    Hi, Tink.

    Thank you for the nice upload tool.

    I was wondering if you are aware of file size limitations. I am successfully uploading small files (1MB is fine). However, when I try to do large files (>10MB) it churns for a long time (as expected) and eventually gives me an “upload successful” message. Yet the file never shows up in the folder. Do you know why this could be happening?

    Thanks a lot.
    Patrick

  50. Tink says:

    Hey Patrick

    The docs state that the Flash Player can upload files of up to 100 MB. So you should be okay with the >10MB files. You’ve tested the small files with the same SWF? A common reason for the upload looking ok, but no file shwoing up on the server is due to the folder not having write permission.

  51. Doug Karr says:

    Not being a flash guru… is it possible then to use drag and drop and actually drop the file you wish to upload?

    Thanks!
    Doug

  52. Tink says:

    Hey Doug

    Unfortunately not. I believe Apollo will give that functionality.

  53. Bill says:

    Unfortunately, capturing that drag event isn’t captured by the native Flash plug-in. (At least it’s not been exposed yet.) Several third party tools like Zinc would provide this functionality.

    But, I’m with Tink – I can’t wait for Apollo!

    Macromedia Apollo (Yes, Macromedia. I haven’t gotten use to Adobe Flash yet. :) )
    http://labs.adobe.com/wiki/index.php/Apollo

  54. getreal says:

    Very usefull. By the way flash 8 has been released. shit stuff u are running here……

  55. Tink says:

    you know how to read the date getreal? This pos is over a year old!

  56. [...] ec 2006 api d’upload de fichiers marco wrote this 3:50 pm: A testerEn flashhttp://www.tink.ws/blog/filereference-example En Ajaxxhttp://www.anyexample.com/programming/php/htt [...]

  57. Brecht says:

    I used the fileReference in one of my latest websites, but you have to try this on MAC OS, it will not work…

  58. Tink says:

    Works fine on a Mac, thats what i have.

    What problem do you have with it?

  59. Neil says:

    Hi Tink,

    Thanks for the example, my test worked fine. I am using it to allow my family to upload images, how do you check if the filename already exists? And if so, re-name the file? Is this done in the PHP script?

    Thanks

    Neil

  60. Tink says:

    Yeah you’d have to check that with PHP.

  61. John says:

    Just wondering if there is a way to return something from the upload call that i could read on the Complete event in flex. Uploading a file and want to tell Flex the path without having to do another service call. please shoot me an email if you know how to do this

  62. rony says:

    why is the source file empty ? just a .txt-file placed!!!!

  63. Tink says:

    Try reading the comments!!!!

  64. Alfonso says:

    Hi, thank you very much for the movie, it is just what i was needing, i have look for this for all the web, i am very happy for to find it. Sorry i dont know how and where to put the my server phats for upload and download, can you help me with this?
    Thank you very much
    Alfonso

  65. Tink says:

    Hey Alfonso I’m not entirley sure what your stuck on?

  66. Alfonso says:

    Hi Tink, thanks. Sorry i dont speak english very well. I have my web page http://www.vonschonberg.com and i have a folder “images”. Then i want upload and download the images at this Path: http://www.vonschonberg.com/images/
    ¿i need to put this path in anywhere of your php file or your fla file for upload or download the images to this folder into my webserver?
    my upload and donwnload dont work now

  67. Alfonso says:

    In FileReferenceExemple.fla have put:
    //define the path to the server script configured to accept uploads
    this._uploadURL_str = “http://www.vonschonberg.com/upload.php”;

    //open OS window and specify the path to the file to download and the file name that will be displayed
    success = this._fileReference.download(“pic2.jpg”, “pic3.jpg”);

    I need to change anymore? and in upload.php where put i my folder server for to upload? and where put i my folder server for to downolad?

    I have uploaded my .swf page and .swf movies in the root server, and the files from your movie example in the root too

    Sorry, i dont know almost to programer i have try all posibilities, i think, but i dont achieve to enreuter the movie example for to work: The Upload say that the file ist me successfully uploaded but i dont find the file uploaded in my server, and the donload say me Upload failed, dont show me any file for to download from my server to my pC

    Thank you
    Alfonso

  68. Paul Steven says:

    I am getting an error trying to upload a file.

    I have ensured the uploadedFiles directory has the correct permissions (777)

    Basically it is displaying the loading progress bar until it reached 100% then I get the following error:

    onHTTPError: site.jpg
    ////////////////////////////////////////////////////////////////////////////

    I have tried with different files and the result is the same.

    I am on Windows Vista Ultimate and testing in Firefox 2.0.0.4

    Any ideas why I am experiencing this problem?

    Could it be anything to do with the fact my server uses the following directory structure?

    /httpdocs/uploadedfiles/

    All advice much appreciated! I need to get this working so I can then go on to try and create a desktop app that allows users to drag files onto the application to upload them. If I can’t even get this to work I have no hope!!

  69. Mat says:

    Amazing, love the idea and the execution. Thanks very much. I am having only one issue.

    When the file is uploaded, it is given the permissions 600 (user = rw-, group = —, everyone = —)… Unfortunately the file is owned by Wheel, thus forcing me to chmod and chown each and every time someone uploads a file.

    Not sure if this is OS X giving default file permissions, or something on the behalf of flash, but I any advice would be greatly appreciated. If this particular issue is covered in the book, please let me know and I will happily purchase a copy for my self.

    Regards,
    Mat

  70. Mat says:

    my bad, the owner is www, and the group is wheel.

  71. I´ve tried get the path in Flash of file i´ve selected… but i can only have the name with the filereference.name.
    Do you know how i can get the path of file i´ve selected in flash (maybe some code in the php part ou something like that???
    Thanks and congratulations for the good job!!!

  72. Tink says:

    You can’t get the path for security reasons.

  73. tiny says:

    Looks great, but, and this seems to be some standard that coders use, why are the most essential pieces of info used to actually make this work so mysterious.
    I’m no expert so maybe I’m missing something obvious to some.
    Two glaring questions :
    Where oh where do I set the path for the folder to upload to? Some php file?
    Where oh where do I set the path for the folder to download from? It seems you have it set to download a specific single file?

    thanks,
    t…

  74. tiny says:

    OK, I see where the upload path is set in the upload.php (it’s set to a folder ‘uploadedFiles’) file provided in the source download, and I’ve set the permissions and it works, yes!

    How to set the download and read from a folder is still a mystery, for me anyway.
    I’m trying but I’m a bit over my head here.

    Any help is much appreciated.
    t…

  75. Tink says:

    You can’t use FileReference to get a list of files in a directory for the download process (or upload for that matter, although as you know you do get to browse on upload).

    You could call a PHP script that looks in a folder and returns alist of the files it contains, then display ths is flash, and download the one the user chooses.

    function dirList ($directory)
    {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

    // if $file isn’t this directory or its parent,
    // add it to the results array
    if ($file != ‘.’ && $file != ‘..’)
    $results[] = $file;
    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

    }

  76. tiny says:

    Thanks tink, I’ll give it a try and post my results.

  77. tiny says:

    Well I’m just lost in space and very frustrated. I gave it my all but I am just not capable of making the flash call the php and return a list. I’m gonna have to try something else, thanks for the effort.
    t…

  78. Houwen says:

    Is it possible to set the target folder for just once when downloading multiple files without letting a user gets prompted for each file he is downloading, assuming that the default filenames are always available?

  79. thanks for the GREAT post! Very useful…

  80. Titus says:

    Can we download the source for this yet :)
    Thanks so much and God Bless.
    Titus Blair

  81. suresh says:

    Hi Tink!
    I just saw your example, but this example only feature single file upload would you give me a code or source for multiple file upload with file reference object, since iam using flash 8 and as 2.0

  82. Jorge says:

    I just tried your example and it worked all right untill I try to upload a file larger than 8 MB I already tweaked the php.ini to allow larger files but it just doesn’t!
    The flash progress and message says the file is uploaded but it isn’t.

    Any ideas?

  83. Jorge says:

    Never minds, sorry to bug you I didn’t realised I had to allow larger POST data in the PHP.INI and it just worked beautifully!

    Thanks a million for your script, it does wonders!

  84. jim says:

    hi,

    I discover flah,
    where can i find the source code of this filereference exemple script ?

    thanks

  85. jim says:

    Because In the zip file, there is only a single txt file

    best regards

  86. same here, the txt file ironically says:

    “Sorry not source until Flash 8 is released.”

    pretty cool demo, otherwise . Congratz !:

  87. Rafael says:

    Hello,

    How do I upload a server asp?

    Abs,

    Rafa

  88. bojkar says:

    Progres bar dasn’t work, its complite by 2 second but file is stilling upload :/ What is wrong?

  89. Ammad says:

    hi tink

    i am a student and new to flash builder 4 . my project manager ask me to build a p2p application in which server send a list of videos to client…. both applications should play videos in the page,,, i manage to play one video and then send it to the client which client receive fine,,, but when i start to load() a list of videos through FileReferenceList class ,,, it do nothing ,,,, even it dont display the video titles etc ,,,,, my project deadline is near ,,, if u guide me in this regard i will be manage to complete the project ,,,, with deeply thanks,
    i will be waiting for ur response
    plz,,,,

    Ammad

Leave a Reply