使用laravel與ajax怎么實(shí)現(xiàn)一個文件上傳功能-創(chuàng)新互聯(lián)

這篇文章給大家介紹使用laravel與ajax怎么實(shí)現(xiàn)一個文件上傳功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)專注于中大型企業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站制作和網(wǎng)站改版、網(wǎng)站營銷服務(wù),追求商業(yè)策劃與數(shù)據(jù)分析、創(chuàng)意藝術(shù)與技術(shù)開發(fā)的融合,累計(jì)客戶千余家,服務(wù)滿意度達(dá)97%。幫助廣大客戶順利對接上互聯(lián)網(wǎng)浪潮,準(zhǔn)確優(yōu)選出符合自己需要的互聯(lián)網(wǎng)運(yùn)用,我們將一直專注成都品牌網(wǎng)站建設(shè)和互聯(lián)網(wǎng)程序開發(fā),在前進(jìn)的路上,與客戶一起成長!

示例代碼


@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
 //生成路徑,圖片存儲
  $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) {
   //生成路徑,圖片存儲
   $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)一個文件上傳功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)頁名稱:使用laravel與ajax怎么實(shí)現(xiàn)一個文件上傳功能-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://bm7419.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)站、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)