我有一个包含一个文件上传的表单。
表单ID为“upload_form”
<input type="file" id="image" name="image"/>
使用javascript onclick函数和ajax将图像传递给控制器。
Ajax fn:
$.ajax({
url: 'UploadImage',
data:new FormData($("#upload_form")[0]),
type: "post",
dataType:"JSON",
async:false,
success: function (data) {
console.log(data);
}
});
}
路线:
Routes::post('UploadImage','UploadController@Upload');
UploadController:
public function Upload()
{
$file = Input::file('image');
$tmpFilePath = '/temp/uploads/';
$tmpFileName = time() . '-' . $file->getClientOriginalName();
$path = $tmpFilePath.$tmpFileName;
$data_file = $file->move(public_path() . $tmpFilePath, $tmpFileName);
// Error for move() and getClientOriginalName() functions.
}
答案 0 :(得分:2)
使用它,它应该适合你......:)
<form action="" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="file" id="image" name="image"/>
<input type="submit" value="Upload" name="submit">
</form>
答案 1 :(得分:1)
谢谢@GONG。 把我的表格改成了这个。工作
<form enctype="multipart/form-data" id="upload_form" role="form" method="POST" action="" >