我使用骨干作为java脚本框架。我有一个表格,我想同时上传最多10张图片。我也给了enctype =" multipart / form-data"在表单标签中并使用
<form id="myform" enctype="multipart/form-data" method="post">
<input type="file" name="file1"/>
<input type="file" name="file2"/>
</form>
在文件更改时,我使用HTML5 FileReader()来读取文件。然后,当我提交表单时,我想保存所有的formdatas。我必须将这些图片上传到&#39;上传/图片&#39;目录。正如我所提到的,我正在使用骨干,所以当我的表单提交时,我使用e.preventDefault()阻止页面重新加载。之后,
var savemyForm = Backbone.View.extend(){
id: '#form_id",
events: {
'submit' : 'save'
}
save : function (e){
e.preventDefault();
var newproject = new App.Models.Project;
newproject.save({
// I have to pass values here. But how can i pass values of files.
// And that files where I am passing from here should be
// moveable to my '/uploads/directory' using the
// move_upload_file($destination_path, $filename);
});
});
答案 0 :(得分:1)
您应该能够将包含文件的AJAX帖子从多部分表单发送到处理文件写入的服务器端脚本!使用jQuery.forms插件是一件很聪明的事情,所以你的文件上载也是IE兼容的。
然后将表单发布到路由并使用Laravel服务器端脚本处理它:
public function create() {
$file = Input::file('file');
$file_upload = new FileUpload;
$file_upload->name = Input::get('name');
$file_upload->path = $this->settings['destination_folder'];
$file_upload->extension = $file->getClientOriginalExtension();
$file_upload->mimetype = $file->getMimeType();
$file_upload->file_size = $file->getSize();
if (exif_imagetype($file)) {
list($width, $height) = getimagesize($file);
$file_upload->dimensions = $width . " x " . $height;
}
$file_upload->save();
$file->move($this->settings['destination_folder'], $file_upload->name);
}
这是我用来在Laravel中编写文件的代码的嗤之以鼻。在你的情况下,你必须创建一些for循环来添加多个文件。
有关如何在Laravel中设置和使用RESTful控制器的一些信息:
我建议你首先尝试成功上传一个文件,如果有效,请尝试多个。从一到多不是火箭科学。 ;)