As you are writing an Ajax application, you may want to allow users to upload files through an Ajax form. The folks over at AnyExample.com have put together a good tutorial on the subject. The tutorial uses PHP for the back end, but the principal is the same for any server side language.
Below is an excerpt from the tutorial.
How IFRAME file uploading works?
- There is a simple <form... which contains only <input type="file" ... > field.
Target for this form is a hidden IFRAME (with "display: none;" CSS style) and
OnChange event for the file field is set to JavaScript function which checks file extension (optional for this example, but
very useful in general) and submits form.- Special part of the script (marked FILEFRAME, see comments) saves file upload, checks for uploading errors and outputs JavaScript code to that
hidden IFRAME.
The javascript code uses parent.window.document object, which allows to modify parent document (visible page, which users is viewing).
It sets filename value and enables submit button on the other form using getElementById method.- The other form has 'description' text-field and hidden field 'filename'. User may fill 'description' field while file is uploading.
When file uploading is finished, user press submit and "file information" page is generated (based on filename from hidden field and user's
file description).Possible drawback of this method is file garbage: files are uploaded even if user does not press submit button. You may need to write 'garbage file
collector' which will delete any unused file.This example stores all uploaded file in filesystem folder. You need to specify it at the beginning of script, see variables $upload_dir and
$web_upload_dir. There is fail-checking which checks whether it is possible to write create files in upload directory.We use following functions in this example:
PHP
- move_uploaded_file - move file uploaded to web server
- fopen - open file
- fwrite - write to opened file
- fclose - close file
- str_replace - replace one substring by another
- filesize - returns file size in bytes
- filemtime - returns file modification time
You can read the full post here.
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket

Recent comments
4 weeks 1 day ago
5 weeks 3 days ago
6 weeks 4 days ago
6 weeks 5 days ago
7 weeks 5 hours ago
7 weeks 2 days ago
7 weeks 3 days ago
9 weeks 5 days ago
10 weeks 22 hours ago
10 weeks 1 day ago