Laravel 5 Ajax 文件/圖片上傳
php問題
html5模板網
Laravel 5 Ajax File/Image Upload(Laravel 5 Ajax 文件/圖片上傳)
本文介紹了Laravel 5 Ajax 文件/圖片上傳的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我的 Laravel ajax 應用程序有問題,
我無法通過 ajax POST 上傳圖片/文件.
這是我的代碼.
阿賈克斯..
/*添加新的分類事件*/$(".addbtn").click(function(){$.ajax({url:'添加目錄',數據:{logo:new FormData($("#upload_form")[0]),},數據類型:'json',異步:假,類型:'帖子',過程數據:假,內容類型:假,成功:功能(響應){控制臺日志(響應);},});});/*添加新的類別事件*/
刀片模板...
</表單>
<div class="modelFootr"><button type="button" class="addbtn">添加</button><button type="button" class="cnclbtn">重置</button>
控制器..
公共函數catadd(){if (Input::hasFile('logo')){返回文件存在";}別的{返回文件不存在";}}
路線..
Route::post('add-catagory',['as'=>'catagory_add','uses'=>'MastersController@catadd']);
我的代碼有什么錯誤???
我無法在 Laravel 控制器中獲取文件信息..
我該如何解決這個問題...?
解決方案
有兩點需要改變:
從以下位置更改您的 js 文件:
數據:{logo:new FormData($("#upload_form")[0]),},
致:
data:new FormData($("#upload_form")[0]),
因為您想發送整個表格.
在您的 html 中:
為您的文件輸入字段添加名稱
致:
I have an issue in my laravel ajax application,
I cant upload images/files through ajax POST.
here is my code.
Ajax..
/*Add new catagory Event*/
$(".addbtn").click(function(){
$.ajax({
url:'add-catagory',
data:{
logo:new FormData($("#upload_form")[0]),
},
dataType:'json',
async:false,
type:'post',
processData: false,
contentType: false,
success:function(response){
console.log(response);
},
});
});
/*Add new catagory Event*/
Blade template ...
<form enctype="multipart/form-data" id="upload_form" role="form" method="POST" action="" >
<div class="form-group">
<label for="catagry_name">Name</label>
<input type="hidden" name="_token" value="{{ csrf_token()}}">
<input type="text" class="form-control" id="catagry_name" placeholder="Name">
<p class="invalid">Enter Catagory Name.</p>
</div>
<div class="form-group">
<label for="catagry_name">Logo</label>
<input type="file" class="form-control" id="catagry_logo">
<p class="invalid">Enter Catagory Logo.</p>
</div>
</form>
</div>
<div class="modelFootr">
<button type="button" class="addbtn">Add</button>
<button type="button" class="cnclbtn">Reset</button>
</div>
</div>
Controller ..
public function catadd(){
if (Input::hasFile('logo'))
{
return "file present";
}
else{
return "file not present";
}
}
Route ..
Route::post('add-catagory',['as'=>'catagory_add','uses'=>'MastersController@catadd']);
What is the error in my code ???
I cant get the file information in laravel controller..
How can i solve this issue...?
解決方案
Two things to change:
Change your js file from:
data:{
logo:new FormData($("#upload_form")[0]),
},
To:
data:new FormData($("#upload_form")[0]),
Because you would like to send the whole form.
In your html:
Add a name to your file input field
<input type="file" class="form-control" id="catagry_logo">
To:
<input type="file" name="logo" class="form-control" id="catagry_logo">
這篇關于Laravel 5 Ajax 文件/圖片上傳的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!