Search code examples
file-uploadserverside-javascriptringojs

ringojs fileupload example


Does anyone have an example of uploading a file to the server using ringojs?


Solution

  • There's a simple upload example in the demo app, but it stores uploads in-memory which is not a good idea for most apps. To save uploads to a temporary file you'll currently have to do something like this (this is a modified version of the upload demo action):

    var fu = require("ringo/webapp/fileupload");
    
    function upload(req) {
        if (fu.isFileUpload(req.contentType)) {
            var params = {};
            fu.parseFileUpload(req, params, req.charset, fu.TempFileFactory);
            return {
                status: 200,
                headers: {"Content-Type": "text/plain"},
                body: [params.file.name, " saved to ", params.file.tempfile]
            };
        }
        return Response.skin(module.resolve('skins/upload.txt'), {
            title: "File Upload"
        });
    }
    

    Unfortunately, there was a bug with saving uploads to temp files that I just fixed, so you'll have to use a current git snapshot or patch file modules/ringo/webapp/fileupload.js manually:

    http://github.com/ringo/ringojs/commit/1793a815a9ca3ffde4aa5a07c656456969b504f9

    We also need some high level way of doing this for the next release (e.g. setting a req.uploadTempDir property). I'll open an issue for this.