這篇文章給大家介紹使用laravel與ajax怎么實(shí)現(xiàn)一個(gè)文件上傳功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
示例代碼
@extends('layouts.art') @section('content') <form class="form-horizontal" id="avatar"> {{ csrf_field() }} <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">名字</label> <div class="col-sm-8"> <input type="title" class="form-control" id="title" name="title"> </div> </div> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">頭像</label> <div class="col-sm-8"> <input type="file" class="form-control" id="title" name="photo"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <a class="btn btn-default" onclick="uploadInfo()">上傳</a> </div> </div> </form> <script src="/tupian/20230522/script> <script type="text/javascript"> function uploadInfo() { var formData = new FormData($("#avatar")); $.ajax({ url: "{{url('photo')}}", type: 'POST', data: formData, contentType: false, processData: false, success: function (returndata) { console.log(returndata); }, error: function (returndata) { console.log(returndata); } }); } </script> @endsection
//生成路徑,圖片存儲(chǔ) $ext = $request->photo->getClientOriginalExtension(); $cover_path = "images/album/covers/" . time() . $ext; $name = "photo".time(); $src = "images/album/photos/". $name .".jpg"; Image::make($request->photo)->save(public_path($src));*/ //批量上傳圖片 foreach ($request->photo as $key => $value) { //生成路徑,圖片存儲(chǔ) $prefix = "photo".mt_rand(1,1000); $Origname = $value->getClientOriginalName(); $name = $prefix.$Origname; $src = "images/album/photos/".$name; $realPath = $value->getRealPath(); Storage::disk('uploads')->put($src,file_get_contents($realPath)); }
關(guān)于使用laravel與ajax怎么實(shí)現(xiàn)一個(gè)文件上傳功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁名稱:使用laravel與ajax怎么實(shí)現(xiàn)一個(gè)文件上傳功能-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://aaarwkj.com/article8/ihjip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、定制網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容