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

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

问题描述

我只想通过 REST API 组织工作流.我有一个允许上传图像的表单(enctype="multipart/form-data").我如何通过主干处理这个表格?请帮助我,我如何将其序列化为带有文件字段的 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.

谢谢.活力

推荐答案

如果您使用的是 HTML5,您可以使用文件 api 中的 readAsDataURL 方法来读取它并将其存储在您的模型上.

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

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

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