Note:
Although the demo implementations contained in this repository include source files from remote servers, it is recommended to download all dependencies and upload them to your own server.
This excludes script files hosted on Google's Content Delivery Network, which is a more reliable source than GitHub demo pages.
The provided example implementation works out of the box and only needs one step for you to add it to your PHP based website:
Visit the uploaded directory - you should see the same file upload interface as the demo, allowing you to upload files to your website.
If uploading files doesn't work, make sure that the php/files directory permissions allow your server write access.
Note:
Please keep in mind some Security considerations when running a file upload handler on your server.
Visit the uploaded directory - you should see the same file upload interface as the demo, allowing you to upload files to your App Engine instance.
You can install the sample Node.js upload service on your server via npm:
npm install blueimp-file-upload-node
You can start the service by running the following command:
./node_modules/blueimp-file-upload-node/server.js
Next, download the plugin archive, extract it, and adjust the url option in main.js to the url of your Node.js service (e.g. "http://localhost:8080").
You can then upload the project folder (without the unnecessary server subfolder) to any static file server and use it as interface to yourNode.js upload service.
Make sure to have imagemagick CLI tools installed on the server running the node upload service.
{"files": [ { "name": "picture1.jpg", "size": 902604, "url": "http:\/\/example.org\/files\/picture1.jpg", "thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture1.jpg", "deleteUrl": "http:\/\/example.org\/files\/picture1.jpg", "deleteType": "DELETE" }, { "name": "picture2.jpg", "size": 841946, "url": "http:\/\/example.org\/files\/picture2.jpg", "thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture2.jpg", "deleteUrl": "http:\/\/example.org\/files\/picture2.jpg", "deleteType": "DELETE" } ]}
To return errors to the UI, just add an error property to the individual file objects:
{"files": [ { "name": "picture1.jpg", "size": 902604, "error": "Filetype not allowed" }, { "name": "picture2.jpg", "size": 841946, "error": "Filetype not allowed" } ]}
When removing files using the delete button, the response should be like this:
{"files": [ { "picture1.jpg": true }, { "picture2.jpg": true } ]}
Note that the response should always be a JSON object containing a files array even if only one file is uploaded.
Visit the uploaded directory - you should see the same file upload interface as in the demo, allowing you to upload files to your website.
The file upload plugin makes use of an Iframe Transport module for browsers like Microsoft Internet Explorer and Opera, which do not yet support XMLHTTPRequest file uploads.
Iframe based uploads require a Content-type of text/plain or text/html for the JSON response - they will show an undesired download dialog if the iframe response is set to application/json.
You can make use of the Accept header to offer different content types for the file upload response. Here is the (PHP) example code snippet for the Accept content-type variation:
<?php header('Vary: Accept'); if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) { header('Content-type: application/json'); } else { header('Content-type: text/plain'); } ?>
Here is a Ruby on Rails example to serve the proper Content-Type:
def update_attachment name = params[:attachment_name] style = params[:attachment_style] image = params[:user][name] raise "No attachment #{name} for User!" unless User.attachment_definitions[name.to_sym] current_user.update("#{name}" => image) render(json: current_user.to_fileupload(name, style), content_type: request.format) end
Thanks to the content_type
option of render, the correct header is set for both IE and true browsers.
For the record, here is the to_fileupload
method:
def to_fileupload(attachment_name, attachment_style) { files: [ { id: read_attribute(:id), name: read_attribute("#{attachment_name}_file_name"), type: read_attribute("#{attachment_name}_content_type"), size: read_attribute("#{attachment_name}_file_size"), url: send(attachment_name).url(attachment_style) } ] } end