我如何序列化要上传的表单文件,以便可以使用Backbone.js,jQuery和Node.js将其存储在mongo数据库中?

乔·c

我正在尝试构建文件上传器,以便申请人可以上传其简历以用于特定职位。我可以获取除文件上载输入以外的所有表单值。如何上传文件,对其进行序列化并将其存储在mongodb文档中(而不是服务器上的文件夹中)?

模板:

<form id="applicants" enctype="multipart/form-data">
    <label for="firstName">First Name:</label> <input type="text" id="first-name" /><br/>
    <label for="lastName">Last Name:</label> <input type="text" id="last-name" /><br/>
    <label for="email">Email:</label> <input type="text" id="email" /><br/>
    <label for="phone">Phone:</label> <input type="text" id="phone" /><br/>
    <br/>
    <label for="resume">upload resume</label><input type="file" id="resume" /> | <a href="#employment"><< Back</a>
    <input type="submit" value="submit resume" id="submit-resume">
</form>

看法:

var Marionette = require('backbone.marionette');

module.exports = itemView = Marionette.ItemView.extend({
    initialize: function() {
        // console.log("jobDetails itemView init");
        this.listenTo(this.model, 'change', this.render);
        // console.log("view's model ._id: ", this.model.attributes.get('_id'));
    },
    events: {
        'click #submit-resume': 'save'
    },

    template: require('../../templates/job_post_details.hbs'),

    save: function(e) {
        e.preventDefault();
        //console.log(this.model.attributes.get('_id'));

        var newApplicant = {
            firstName: this.$el.find('#first-name').val(),
            lastName: this.$el.find('#last-name').val(),
            phone: this.$el.find('#phone').val(),
            email: this.$el.find('#email').val(),
            jobPosts: [ this.model.attributes._id ],
            resume: this.$('#resume').submit() // how to I obtain this data and serialize?
        };

        console.log("newApplicant: ", newApplicant);


        window.App.controller.newApplicantCreate(newApplicant);
    }
});

服务器端代码:

app.post('/api/applicants', job_post_applicants.add); 

路由触发器添加以下控制器中的功能:

    add: function( req, res, next ) {
        console.log("req: ", req);
        console.log("res: ", res);

        var newApplicant = new models.Applicant( req.body );
        newApplicant.save( function( err, Applicant ) {
            if (err) {
                res.json({ error: 'Unable to create new Applicant.' });
            } else {
                res.json(Applicant);
                console.log("added the Applicant: ", Applicant);
            }
        });
    }
};

这是日志:

POST /api/applicants 200 37ms - 238b
added the Applicant:  { __v: 0,
  firstName: 'John',
  lastName: 'Doe',
  phone: '555.555.5555',
  email: '[email protected]',
  _id: 544eb24e9698b900000b5de9,
  resume: {},  <=== how to I populate with serialized data?
  createdAt: Mon Oct 27 2014 16:59:58 GMT-0400 (EDT),
  jobPosts: [ 544995891887bd411b4f02ee ] }
威廉·福汀

您可以使用FormData对象并将其作为数据发送。

var form = new FormData(); 
form.append('firstName', this.$el.find('#first-name').val());
//... append all data
// Append resume file
form.append("resume", $("#resume")[0].files[0]);

并将其作为“多部分/表单数据”内容类型发送。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档