Backbone.js的链接文件的模型 [英] Backbone.js link file to model

查看:253
本文介绍了Backbone.js的链接文件的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用Django和Backbone.js的Web应用程序。问题是我需要将文件上传到服务器。我怎样文件链接骨干模式?因此,当我执行model.save()将文件上传到服务器。

I am creating a web application using django and backbone.js. The problem is I need to upload files to the server. How do I link the backbone model with a file? Thus when I execute model.save() the file is uploaded to the server.

编辑:只是为了把事情说清楚。我想要做的就是我要与骨干模型链接输入文件框。因此,当用户从他/她的计算机中选择一文件,我应该能够在该文件链接与骨干模型。当我打电话model.save()中的骨干脚本应该与模型的其余部分一起发送的文件。

just to make things clear. What I want to do is I want to link a input file box with the backbone model. So when the user selects a file from his/her computer I should be able to link that file with the backbone model. And when I call the model.save() in the backbone script it should send the file along with rest of the model.

推荐答案

的问题是,你不能直接使用文件上传使用AJAX。
常见的解决方法是将表单提交给一个隐藏的iframe。

The problem is that you can't use file upload with AJAX directly. The common workaround is to submit a form to a hidden iframe.

您可以使用形式的jQuery插件。它提供ajaxSubmit会的方法,它的工作原理就像jQuery.ajax通话,但使用隐藏的iframe,因此它可以上传文件。

You can use jQuery Form plugin. It provides ajaxSubmit method, it works like jQuery.ajax call but uses hidden iframe so it can upload files.

此外,你需要重写Backbone.sync或每个型号都有所覆盖的同步和$替换$阿贾克斯调用(someForm).ajaxSubmit调用

Also you need to override Backbone.sync or override sync on a per-model basis and replace $.ajax call with $(someForm).ajaxSubmit call

在服务器,你应该返回JSON字符串id参数和文件参数包含文件网址

On server you should return JSON string with id parameter and file parameter containing file url

这篇关于Backbone.js的链接文件的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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