Laravel Image upload Ajax Way

Source: Internet
Author: User
Tags button type

Laravel image Upload

Background Carousel Map Upload $ ("#img-upload"). On (' Submit ', function (e) {e.preventdefault (); var formData = new FormData ($ (". Banner-upload ")); $.ajax ({headers: {        ' X-csrf-token ': $ (' meta[name= ' Csrf-token"] '). attr (' content ')    },    type: ' POST ',    URL: '/admin/banner/create ',  data:formdata,  processdata:false,  //mimetype: "Multipart/form-data ",  Contenttype:false,            cache:false,  success:function (data) {    console.log (data);    if (data.status) {    console.log (data.message);    }    },    error:function (err) {    Console.log (err );    }});  

  

Form form Mode

<form class= "Form-horizontal" action= "{{URL ('/admin/banner/create ')}}" method= "POST" enctype= "multipart/ Form-data "class=" Banner-upload "> {{Csrf_field ()}} <div class=" Box-body "> <div class=" for M-group "> <label for=" inputEmail3 "class=" Col-sm-2 Control-label "> Topics </label> <div clas          s= "col-sm-10" > <input type= "text" class= "Form-control" value= "" name= "Theme" placeholder= "Set Carousel Theme" > </div> </div> <div class= "Form-group" > <label for= "InputPassword3" class = "Col-sm-2 Control-label" > Status </label> <div class= "col-sm-10" > <input type= "Radio" Val Ue= "1" name= "status" > Enable <input type= "Radio" value= "0" name= "status" > Disable </div> &L t;/div> <div class= "Form-group" > <label for= "inputPassword3" class= "Col-sm-2 Control-label" ; Upload pictures </label> <divclass= "col-sm-10" > <input type= "file" name= "photo" value= "" placeholder= "" > <div class= " Img-wrap ">  </div> </div> </div> &l T;/div> <!--/.box-body-<div class= "Box-footer clearfix" > <button type= "Submit" CLA ss= "btn btn-info pull-right" id= "Img-upload" > Submit </button> </div> </form>

Background functions

Public Function bannercreate (Request $request) {$file = $request->file (' photo ');//Get Picture $theme = $requ        est->theme;//Theme $status = $request->status;//status $allowed _extensions = ["png", "JPG", "gif"]; if ($file->getclientoriginalextension () &&!in_array ($file->getclientoriginalextension (), $allowed _  Extensions) {return response ()->json ([' status ' = = False, ' message ' = = ' Upload png only | JPG |        Images in GIF format ']);        } if ($request->hasfile (' photo ')) {} $destinationPath = ' storage/uploads/';        $extension = $file->getclientoriginalextension (); $fileName = Str_random (10). '. '.        $extension;        $file->move ($destinationPath, $fileName); Return Response ()->json ([' status ' = = True, ' pic ' = Asset ($destination             Path. $fileName), ' ismake ' = $status,   ' Message ' + ' added success!    ‘ ] ); }

  $destinationPath‘storage/uploads/‘;    后面必须要有 / 负责上传后路径访问有问题

<form class= "Form-horizontal" action= "{{URL ('/admin/banner/create ')}}" method= "POST" enctype= "multipart/ Form-data "class=" Banner-upload ">

{{Csrf_field ()}}

<div class= "Box-body" >
<div class= "Form-group" >
<label for= "InputEmail3" class= "Col-sm-2 Control-label" > Themes </label>
<div class= "Col-sm-10" >
<input type= "text" class= "Form-control" value= "name=" theme "placeholder=" Set Carousel Theme ">
</div>
</div>

<div class= "Form-group" >
<label for= "InputPassword3" class= "Col-sm-2 Control-label" > Status </label>
<div class= "Col-sm-10" >
<input type= "Radio" value= "1" name= "status" > Enabled
<input type= "Radio" value= "0" name= "status" > Disabled
</div>
</div>

<div class= "Form-group" >
<label for= "InputPassword3" class= "col-sm-2 Control-label" > Uploading images </label>
<div class= "Col-sm-10" >
<input type= "file" name= "photo" value= "placeholder=" ">

<div class= "Img-wrap" >

</div>
</div>
</div>

</div>
<!--/.box-body--
<div class= "Box-footer clearfix" >
<button type= "Submit" class= "btn btn-info pull-right" id= "Img-upload" > Submit </button>
</div>
</form>

  

Laravel Image upload Ajax Way

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.