Home  >  Article  >  Web Front-end  >  jQuery encoding to convert base64 upload via AJAX

jQuery encoding to convert base64 upload via AJAX

php中世界最好的语言
php中世界最好的语言Original
2018-04-28 13:55:012172browse

This time I will bring you jQuery encoding conversion base64 upload through AJAX, jQuery encoding conversion base64 upload through AJAX What are the precautions, the following is a practical case, let's take a look.

The example in this article describes how jQuery encodes files into base64 and uploads them through AJAX. Share it with everyone for your reference, the details are as follows:

It is impossible to upload files directly using AJAX. Generally, a new iframe is created to complete the form submission process in it to achieve the effect of asynchronous file upload.

This can achieve better browser compatibility, but the amount of code will be relatively large, even if file upload plug-ins are used, such as plupload.

How can we achieve a level of flexibility? It would be great if we could treat files as ordinary form parameters just like ordinary AJAX submission of form data.

I had a flash of inspiration, wouldn’t it be enough to use the FileReader object of javascript to encode the file into base64 and then transmit it to the server~

Start doing it and have enough food and clothing. .

The front-end base64 encodes the file and transmits it to the server through ajax:

<head>
  <meta charset="UTF-8">
</head>
<form onsubmit="return false;">
  <input type="hidden" name="file_base64" id="file_base64">
  <input type="file" id="fileup">
  <input type="submit" value="submit" onclick="$.post(&#39;./uploader.php&#39;, $(this).parent().serialize());">
</form>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#fileup").change(function(){
    var v = $(this).val();
    var reader = new FileReader();
    reader.readAsDataURL(this.files[0]);
    reader.onload = function(e){
      console.log(e.target.result);
      $('#file_base64').val(e.target.result);
    };
  });
});
</script>

The back-end decodes and saves the file data:

<?php
if (isset($_POST['file_base64'])){
  $file_base64 = $_POST['file_base64'];
  $file_base64 = preg_replace('/data:.*;base64,/i', '', $file_base64);
  $file_base64 = base64_decode($file_base64);
  file_put_contents('./file.save', $file_base64);
}

I believe you have mastered it after reading the case in this article For more exciting methods, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of mapState and mapGetters in vuex

Detailed explanation of the steps to implement partial printing of the page in angular

The above is the detailed content of jQuery encoding to convert base64 upload via AJAX. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn