Javascript 在Laravel中通过Ajax上传文件
我正在尝试通过Laravel中的ajax上传一个文件Javascript 在Laravel中通过Ajax上传文件,javascript,jquery,ajax,laravel,Javascript,Jquery,Ajax,Laravel,我正在尝试通过Laravel中的ajax上传一个文件 $("#stepbutton2").click(function(){ var uploadFile = document.getElementById("largeImage"); if( ""==uploadFile.value){ } else{ var fd = new FormData();
$("#stepbutton2").click(function(){
var uploadFile = document.getElementById("largeImage");
if( ""==uploadFile.value){
}
else{
var fd = new FormData();
fd.append( "fileInput", $("#largeImage")[0].files[0]);
$.ajax({
url: '/nominations/upload/image',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
if(data.uploaded==true){
alert(data.url);
}
},
error: function(err){
alert(err);
}
});
}
});
我正在将文件输入传递给php脚本
public function image(){
$file = Input::file('fileInput');
$ext = $file->getClientOriginalExtension();
$fileName = md5(time()).".$ext";
$destinationPath = "uploads/".date('Y').'/'.date('m').'/';
$file->move($destinationPath, $fileName);
$path = $file->getRealPath();
return Response::json(["success"=>true,"uploaded"=>true, "url"=>$path]);
}
我得到的答复是
{"success":true,"uploaded":true,"url":false}
请求有效负载为
------WebKitFormBoundary30GMDJXOsygjL0ZS
Content-Disposition: form-data; name="fileInput"; filename="DSC06065 copy.jpg"
Content-Type: image/jpeg
为什么会发生这种情况?找到了答案:
public function image(){
$file = Input::file('fileInput');
$ext = $file->getClientOriginalExtension();
$fileName = md5(time()).".$ext";
$destinationPath = "uploads/".date('Y').'/'.date('m').'/';
$moved_file = $file->move($destinationPath, $fileName);
$path = $moved_file->getRealPath();
return Response::json(["success"=>true,"uploaded"=>true, "url"=>$path]);
}
将路径分配给新变量后获取路径 在移动文件之前,请尝试获取路径。响应为
{“success”:true,“upload”:true,“url:“C:\\wamp\\tmp\\php7A29.tmp”}
和。。。这不是你想要的吗?我想将文件上传到一个文件夹,并将URL返回到ajax。{“success”:true,“upload”:true,“URL”:“\/uploads\/2015\/02\/”}