【发布时间】:2017-05-12 06:14:01
【问题描述】:
在我的 laravel 控制器中,当我尝试上传图片时出现此错误..
我知道问题出在哪里 这是在我的控制器里面
$image = $request['images'];
if ($image)
{
$imgfile = $request['images'];
$imgpath = 'uploads/users/images/'.$user->id;
File::makeDirectory($imgpath, $mode = 0777, true, true);
$imgDestinationPath = $imgpath.'/'; // upload folder in public directory
$ext1 = $imgfile->getClientOriginalExtension();
$filename1 = uniqid('vid_').'.'.$ext1;
$usuccess = $imgfile->move($imgDestinationPath, $filename1);
}
这是我的 jquery 代码,我的表单 ID 是更新配置文件,我通过其名称获取所有字段值
$("#updateprofile").on('submit', function(e){
e.preventDefault(e);
var redirect_url = $(this).find("[name='redirect_url']").val();
var url = $(this).attr('action');
var method = $(this).attr('method');
var myData = {
name: $(this).find("[name='name']").val(),
gender:$(this).find("[name='gender']").val(),
lastname:$(this).find("[name='lastname']").val(),
email:$(this).find("[name='email']").val(),
mobile:$(this).find("[name='mobile']").val(),
address:$(this).find("[name='address']").val(),
city: $(this).find("[name='city']").val(),
state:$(this).find("[name='state']").val(),
country:$(this).find("[name='country']").val(),
pin:$(this).find("[name='pin']").val(),
images: document.getElementById('imageToUpload').files[0],
}
console.log("data", myData);
$.ajax({
type: "PUT",
url: url,
dataType: 'JSON',
data: myData,
success: function(data) {
console.log(data);
//alert("Profile update successfully")
//window.location.href = redirect_url;
},
error: function(jqXHR, textStatus, errorThrown) {
alert("das");
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
});
【问题讨论】:
-
$imgfile是一个字符串,所以$request['images']可能只给你文件名,而不是文件对象本身。根据documentation,您可以通过$request->file('image')访问您的文件。还是有多个文件? -
发布 print_r($request['images']);\
-
是的,但是在给
$request->file('image')时,它会在我的控制台窗口jquery.min.js:3130 Uncaught TypeError: Illegal invocation中向我显示这样的错误 -
Jquery 不会在后端给你一个错误,你似乎向浏览器发送了一些不正确的东西。
-
@TobiasF。我听不懂你在说什么
标签: php jquery ajax image laravel-5.4