Example analysis of multi-image upload function implemented by Laravel framework +blob

Source: Internet
Author: User
This article mainly introduces the Laravel framework +BLOB implementation of the multi-image upload function, combined with the case of a detailed analysis of the Laravel framework +blob Multi-image upload operation of the front-end submission and background processing related operations skills, the need for friends can refer to the next

This paper describes the multi-image uploading function of Laravel framework +blob. Share to everyone for your reference, as follows:

I. INTRODUCTION

We know that multi-image upload is usually accompanied by the instant display function, that is, you can immediately see the uploaded pictures. A multi-image upload plugin that has been used before is the selection of images, click Upload and then the image resources uploaded to the server, and then return to the stored path information, finally we click on the form's submit button and then insert the information into the database.

Now there is an awkward place, and when I click Upload Image, I cancel this form submission again. But the picture resources have been to the server, easy to cause space waste and so on.

Now provide a self-laravel framework to write a multi-image upload, (of course, can be directly applied anywhere), the feature is: Image upload can be displayed immediately, but is the browser through the BLOB call cache image information, when the form submitted, the picture resources will really upload server and database.

Two. Front End

Note: This example is based on the Laravel framework

On the form form first

<form method= "POST" enctype= "Multipart/form-data" action= "#" >  {{Csrf_field ()}}  <ul class= "List_ BTN ">  <li></li>   <li> <input type= "file" id= "House_img_one1" name= "Art_thumb" multiple= "multiple" Onchange= "Houseimgone (This)" ></li></ul>  <p class= "Submit" > Upload </p></form>

JS Code

<script> var _btnid = ';  var all_urls= "";  var all_types= ""; function Houseimgone (_this) {var img = '  ' _btnid = $ (_t    His). attr (' id ');    var obj = document.getElementById ("House_img_one1");    var length = Obj.files.length;       Multi-image upload traversal file information (can be viewed through object.files) for (var i = 0; i < length; i++) {var objurl = Getobjecturl (_this.files[i]);      Picture suffix type splicing all_types=all_types+_this.files[i].type;      Convert the picture to Base64 from the character var ofreader = new FileReader ();      Ofreader.readasdataurl (_this.files[i]); Ofreader.onload = function (ofrevent) {all_urls=all_urls+ofrevent.target.result+ "&| | |";      Splicing the data form base64 URL};        if (Objurl) {$ ('. Sz:last '). Before (IMG);      $ ('. Sz '). EQ ($ (". Sz"). length-2). attr ("src", objurl);    }}}//Click the Submit button to trigger Ajax $ (". Submit"). Click (function () {//console.log (all_types);  $.ajax ({type: "post", url: "{{URL (' admin/img ')}}",    data:{' IMGs ': all_urls, ' types ': all_types, ' _token ': ' {{Csrf_token ()}} '}, DataType: ' JSON ', success:function (data          {if (data==1) {//Layer plugin prompt, you can choose Layer.msg ("Upload success", {Icon:6});        Window.location.reload (); }else {alert ("Upload failed!        ");  }      }    });  });    Gets the Blog object URL (actually gets the picture path information in the cache for immediate display, not the actual resource path returned by the server) function Getobjecturl (file) {var url = null;    if (window.createobjecturl! = undefined) {url = window.createobjecturl (file); } else if (window. URL! = undefined) {url = window.    Url.createobjecturl (file);    } else if (window.webkiturl! = undefined) {url = window.webkitURL.createObjectURL (file);  } return URL; }</script>

Three. Background processing code

Public function Store (Request $request) {  $data = $request->all ();  $imgs = $data [' IMGs '];  Array_values () is used to reset the array subscript  $types =array_values (Array_filter (Explode (' image/', $data [' types ']));  $arr =array_values (Array_filter (Explode (' &| | | ', $IMGS)));  foreach ($arr as $k = + $v) {  //file path  $filepath = Base_path (). ' /storage/app/imgs/'. Date (' Ymdhis '). $k. '. $types [$k];  Extract base64 characters  $imgdata = substr ($v, Strpos ($v, ",") + 1);  $decodedData = Base64_decode ($imgdata);  File_put_contents ($filepath, $decodedData);  Insert Database  $img = new img;  $filepath = STRCHR ($filepath, '/');  $img->img_path= $filepath;  $img->save ();}

Articles you may be interested in:

Swoole 1.10.0 New Release, added a number of new features to resolve

An example of how PHP implements a clockwise print matrix (spiral matrix)

A method for judging the symmetry of binary tree by PHP

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.