骨干。与文件上传表单,该如何处理? [英] Backbone. Form with file upload, how to handle?

查看:121
本文介绍了骨干。与文件上传表单,该如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望通过REST API来组织工作流程。我有一个表格,允许上传图片(ENCTYPE =多部分/表单数据)。如何处理骨干通过这种形式?请帮助我,我怎么可以将其与文件域序列化为JSON。

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

推荐答案

如果您使用的是HTML5,可以使用readAsDataURL方法从文件API来读取并存储在您的模型。

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

这里的code我用它来读取和存储。

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);
    }   
});

这篇关于骨干。与文件上传表单,该如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆