Hello. I have a new blog.

I've moved this blog to the following URL Kerkness.ca. Thank you for visiting, please update your bookmarks.

Thursday, June 26, 2008

Flex Component : Yet Another Flex File Upload Component

There are several Flex upload components floating around and I gave most of them a good look over but I was not able to find one which suited my needs so I decided to make my own. Here it is for you to enjoy.

Click here to view a demo
(demo lets you upload up to 3 files at a time with a max size of 4 megs each)

Click here to view the source

The usage of the component is pretty straight forward. You can set some properties to define what types of files can be uploaded and you direct the 'upload' towards a server side script. In my demo I send the files to a basic PHP script ( which I've included below ). The script handles one file at a time and is expected to report back either the string 'successful' or an error message which is displayed.

Properties

The following public properties can be set to customize the use of this component

  • uploadButtonLabel : String = 'Upload'
    This is the label used for the 'upload' button.

  • selectButtonLabel : String = 'Select File(s)'
    This is the label used for the 'select' button.

  • removeButtonLabel : String = 'Remove Selected File'
    This is the label used for the 'remove' button.

  • maxFileCount : Number = 3
    This sets the number of files a user is allowed to upload

  • maxFileSize : Number = 1
    This sets the maximum file size (in megs) for each individual file. Default is 1 meg.

  • requestUrl : String = ''
    This is the URL for your PHP/ASP/ColdFusion/Other script which you will send the uploaded files to. THIS PROPERTY MUST BE SET !

  • allowOnlyImages : Boolean = false
    If set to 'true' the user will only be able to select image files

  • allowOnlyText : Boolean = false
    If set to 'true' the user will only be able to select text files.

  • allowAllFiles : Boolean = true
    If set to 'true' the user can upload images or text files.

  • barColor : String
    Allows you to style the progress bar
Events

The component has one event 'uploadComplete' which is triggered if all files are uploaded successfully.

The PHP Handler

Following is the source for the simple PHP script which I am using in the demo. In this script I am just confirming that the file was uploaded successfully and then I am deleting it. Since this is a public demo I don't want people filling up my server with unwanted images. In your script you would most likely copy the FILE to a more permanent location and possibly do a little more validation. The script needs to echo the string 'success' or echo an error message.
$hasError = false;
foreach( $_FILES as $i=>$file ){
if ( $file['error'] ){
$hasError = true;
}
/**
* Because this is a public example. I am just going to immediately delete
* the file which was uploaded. In a regular application you would copy the file
* from it's temporary location to it's permament location.
*/
unlink( $file['tmp_name'] );
}

if ( ! $hasError ){
echo('success');
} else {
echo('Stick custom error message here');
}

9 comments:

  1. I am trying to debug an upload issue on the MAC. Your uploader on your site works great. When I use the same code on my server, it will only let me upload jpg (not png), and will get to 100% completed but never trigger the UPLOAD_COMPLETE_DATA event. Because it's the same code and PHP you have listed here, I am wondering if you have done anything or know why your server will work with Mac and mine will not. My specs: Apache 2.2, php 5.2, os=FreeBSD 6.2

    Thanks!!!

    ReplyDelete
  2. Thank you for the comment. I'm not certain why you are seeing these issues to be honest.

    There is a method in the uploader class called getImageTypeFilter() which sets the filter that determines what image files are accepted. I haven't done extensive testing with PNG images.

    Does my demo work on your Mac? I've tested the component with OSX Leopard under Firefox and Safari and tested with XP/Firefox and Ubuntu/Firefox.

    I appreciate any testing or debugging info you can find.

    ReplyDelete
  3. Your demo works great on my Mac. I am also using OSX Leopard. Firefox and Safari work fine with your demo as well. PNGs are just fine. So you can rest easy that it shouldn't be your code, but I thought maybe you could offer your server specs to help target why. I have heard of other people in other places having struggles with flex uploaders on a mac, so this may help to pin-point the issue. Thanks!

    ReplyDelete
  4. My server is an Ubuntu server running PHP5 and Apache2. I have not tested anything with PHP4 so if your server is running PHP4 the issue could be a PHP version issue.

    PHP and Apache were both installed on my server using the apt-get method common to Ubuntu servers.

    I hope that helps. Let me know if you figure anything else out.

    ReplyDelete
  5. Thanks for your help. Rest easy - it's not your code. For anyone who runs into this same problem - not being able to upload on a Mac - it has to do with session info associated with the php file you post to. It looks like a bug with flash, NOT with your component. I was using cakephp and there was a session check before loading the script, and that's what screws it up for flash on the Mac. When I removed that, your uploader worked great. Debugging sucks, but I hope it helps anyone who uses this in the future.

    ReplyDelete
  6. @Anon Thank you for the info. It's a good reminder to look at session hiccups. I have actually run into session issues when working with PHP and Flex in other projects.

    ReplyDelete
  7. Finally a script that works well and is easy to implement. I didn't have to change anything but the request url.

    localhost running
    PHP 5.2.5 / Apache 2 / Flex 3


    Thanks :)

    ReplyDelete
  8. hi kerkness
    great work , i checked the code with my jsp code my configuration is Ubundu 8.04 , Firefox 3.0 , LNX 9,0,124,0 flash . when i select a file ,FileReference.type() returns null. its not working . wat version u r using on ur ubundu server ,flash and firefox

    can u help out of this , my mail id is kandaraja@gmail.com

    we are migrating our OS from winodws to linux and flex2.0 to flex3.0


    thanks

    kandasami raja.c

    ReplyDelete
  9. @kandasami

    Thank you for the comment. I have this component running in a variety of environments. Mostly I am using Ubuntu 8.04 on the server end with PHP5 and have used the component both as a Flex3 component in Firefox 2 and 3, and Safari. Also tested inside an Adobe Air application with no issue.

    Have to say I haven't extensively tested the component while using a Windows machine.

    ReplyDelete

Thank you for the comments.