Extension:JQueryUpload

From Organic Design wiki
Revision as of 18:52, 18 September 2012 by Nad (talk | contribs) (Working implementation)

A job I'm working on requires jQuery file uploads into the wiki using the "attach file" link and also directly into Ajax comments. But they require that the files be able to be renamed before uploading, so I'll need to modify the upload code slightly. Changing the filename part into an input box is simply a matter of changing template-upload (which is index.html in the demo code), but processing the PHP side may be more difficult since there can be many files uploading at once and they can upload in chunks.

The initial test works for single non-chunked files which is simply to adjust server/php/index.php to overwrite the name in the $_FILES array with the new name from the input box added to the form called upload_rename_file.

{{{1}}}

Screen shot of the added form field

File-upload-rename.jpg

Working implementation

The following method works for multiple files and chunks. It's done by posting the rename data as two arrays, one of the original names and one of the new names. These arrays are posted along with each file or chunk so that the PHP can use the original name to find which new name the file or chunk should be renamed to.

First this modification is made to template-upload which adds a hidden input to populate the posted array of original filename data, and changes the displayed text name to a text input with the extension removed and added after the input so it can't be accidentally removed or modified.

{{{1}}}


Then this JavaScript is added to the end of the HTML containing the upload form in its own script element which returns the main part of the filename and the extension part, and are called by the template above.

{{{1}}}


Finally, the main part is the PHP which is at the very start of the form processing script. It first checks if any renaming data is being posted, and if so it scans through the original names array and checks if there's any uploading files with the same name, and if so changes the name to the equivalent entry in the new names array.

{{{1}}}

File icons

We need this to work mainly for PDF files, word documents and Excel spreadsheets, rather than images. The program currently doesn't have any file-extension based icon system which we need for ours, so this section describes how the program can be modified to allow custom icons.