H5 mobile development Ajax uploads multiple Base64 format images to the server,

Source: Internet
Author: User

H5 mobile development Ajax uploads multiple Base64 format images to the server,

Let's just look at the code.

1. Upload components

 <div class="imgbox">      <input id="file" type="file" class="fileupload" accept="image/*" multiple capture="camera" @change="viewimg()"/> </div>

2. display the added Image

Viewimg ($ event) {// obtain the current input tag var currentObj = event. currentTarget; // find the img tag of the image to be previewed, and dynamically generate var img = currentObj. parentNode. children [0]; setImagePreview (currentObj, img); function setImagePreview (docObj, imgObjPreview) {if (docObj. files & docObj. files [0]) {imgObjPreview. style. display = 'block'; imgObjPreview. src = window. URL. createObjectURL (docObj. files [0]) ;}}

3. retrieve images and upload them to the server

// Var inputs = $ ("input. fileupload "); for (var I = 0; I <inputs. length; I ++) {// convert the image to base64 upload var file = inputs [I]. files; if (file [0]) {var reader = new FileReader (); reader. readAsDataURL (file [0]); reader. onload = function (e) {var event = this; console. log (event. result); $. ajax ({type: 'post', url: 'http: // 10.145.0.05/goods/addgoodsbase64', dataType: "json", data: {"base64": event. result,}, success: function (data) {console. log (data );}});}}}

Well, this is the process, and the next step will be handled by the back-end personnel.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.