Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone. Form with file upload, how to handle?

Tags:

backbone.js

I want to organize the workflow only through the REST API. I have a form that allows to upload image (enctype="multipart/form-data"). How do I handle this form via backbone? Help me please, how I can to serialize it into JSON with a file field.

Thanks. Vitaliy

like image 419
moskrc Avatar asked Nov 17 '11 17:11

moskrc


4 Answers

If you are using HTML5, you can use the readAsDataURL method from the file api to read and store it on your models.

Here's the code i use to read and store.

var Image = Backbone.Model.extend({

    readFile: function(file) {
        var reader = new FileReader();
        // closure to capture the file information.
        reader.onload = (function(theFile,that) {
            return function(e) {
                //set model
                that.set({filename: theFile.name, data: e.target.result});

            };
        })(file,this);

        // Read in the image file as a data URL.
        reader.readAsDataURL(file);
    }   
});
like image 147
Anthony Chua Avatar answered Oct 20 '22 00:10

Anthony Chua


You could try the jquery.iframe.transport plugin.

like image 44
Matt Burke Avatar answered Oct 20 '22 00:10

Matt Burke


IMHO, you cannot serialize a file into JSON. If you need to send some data along with the file you can send them as query params with POST method.

www.example.com/upload?param1=value1&param2=value2
like image 24
avrelian Avatar answered Oct 20 '22 00:10

avrelian


There's no good way to submit a file via AJAX. So I wrote a function to fake it--it inserts a secret iframe into your DOM that is never visible but still works as a target to submit your form on, and it installs a function for your response to call that cleans house when the file is uploaded.

Have your upload form's submit button fire this function I wrote. It uses jQuery because it's easy and nice, but in principle that shouldn't be strictly necessary:

function backgroundUpload(form, container) {
    $(container).append('<iframe name="targetFrame" id="targetFrame" style="display: none; height: 0px; width:0px;" ></iframe>');
    $(form).attr('target', 'targetFrame');

    window.backgroundUploadComplete = function() {
        //clear your form:
        $(form).find(':file').val('');
        $(form).find(':text').val('');

        //do whatever you do to reload your screenful of data (I'm in Backbone.js, so:)
        window.Docs.fetch().complete( function() { populateDocs(); });

        //get rid of the target iframe
        $('#targetFrame').remove();
    };
    $(form).submit();
}

Then have your form handler that does your file parsing and saving return the string:

<script>window.parent.backgroundUploadComplete();</script>

Your form can look like:

<form id="uploadForm" method="POST" action="/your/form/processor/url" enctype="multipart/form-data">
<input type="file" name="file"/>
<!-- and other fields as needed -->
<input type="button" onClick="backgroundUpload(this.form, $('#documents'));" value="Upload" />
</form>

(#documents is the div that this form lives in. Could probably be any DOM element, it just needs a home.)

like image 36
Dan Ray Avatar answered Oct 19 '22 23:10

Dan Ray