【发布时间】:2013-06-03 18:19:48
【问题描述】:
我在任何地方找到的答案都没有奏效。我正在尝试扩展“开发 Backbone.js 应用程序”中的示例以上传文件。尽管表单有 enctype="multipart/form-data," request.files 始终未定义。
HTML 格式为:
<form id="addBook" action="..." enctype="multipart/form-data">
<div>
<label for="coverImage">CoverImage: </label><input id="coverImage" name="coverImage" type="file" />
<label for="title">Title: </label><input id="title" type="text" />
<label for="author">Author: </label><input id="author" type="text" />
<label for="releaseDate">Release date: </label><input id="releaseDate" type="text" />
<label for="keywords">Keywords: </label><input id="keywords" type="text" />
<button id="add">Add</button>
</div>
</form>
保存新记录的主干是
addBook: function( e ) {
e.preventDefault();
var formData = {};
var reader = new FileReader();
$( '#addBook div' ).children( 'input' ).each( function( i, el ) {
if( $( el ).val() != '' )
{
if( el.id === 'keywords' ) {
formData[ el.id ] = [];
_.each( $( el ).val().split( ' ' ), function( keyword ) {
formData[ el.id ].push({ 'keyword': keyword });
});
} else if( el.id === 'releaseDate' ) {
formData[ el.id ] = $( '#releaseDate' ).datepicker( 'getDate' ).getTime();
} else {
formData[ el.id ] = $( el ).val();
}
}
});
console.log(formData);
this.collection.create( formData );
}
被调用的节点。
//Insert a new book
app.post( '/api/books', function( request, response ) {
console.log(request.body);
console.log(request.files);
});
coverimage 发送到节点的值是正确的,我只是在 request.files 中没有得到任何东西。我有一个很酷的拖放功能,我想用它来代替,但在我得到这个工作之前我被卡住了。
我尝试了 JQuery-file-upload,但无济于事。
如果我有头发,我会马上拔掉它。
【问题讨论】:
标签: node.js backbone.js upload