Ajax file uploading iframe fallback for expressjs

npm install iframe-file-upload-middleware
4 downloads in the last week
21 downloads in the last month

Ajax file uploading iframe fallback for expressjs

express framework already provides convenient way for uploading files using bodyParser. Ajax file uploading works great at modern browsers but we need a fallback for some "explorers" (yes, i'm watching at you msie) that doesn't support ajax file uploading. Fallback in this case - loading file using iframe. This lightweight (about 50 lines of code without dependencies) middleware provides iframe file uploading support for server side.


server setup, at your app.js

var app = express.createServer();

var iframeFileUpload = require('iframe-file-upload-middleware');
app.post(/^.*\/upload$/, iframeFileUpload.middleware());

now you can bind your handler at some upload url e.g. '/images/upload' and process uploads as you usually do, e.g.

app.post('/images/upload', function(req, res) {
    res.json({filename: path.filename(req.files.image.path)});

client setup using jquery file upload

    url: '/images/upload',
    redirect: 'default',
    dataType: 'json'
}).on('fileuploaddone', function(event, data) {

now you can upload image using file input and recives it's file name to the client. It will work in browsers (using ajax file upload) and in explorer using (iframe file uploading).

npm loves you