Home > Web Front-end > JS Tutorial > JavaScript implements uploading and previewing images without refreshing

JavaScript implements uploading and previewing images without refreshing

巴扎黑
Release: 2017-08-14 13:46:38
Original
1292 people have browsed it

This article mainly introduces in detail the JavaScript function of uploading preview images without refreshing. It has certain reference value. Interested friends can refer to

How to implement the function of uploading without refreshing? Handwritten non-refresh upload requires two things, FormData and FileReader.

FileReader is used for image browsing.

FormData is used for ajax requests.

html code

First create a container for the form and image


##

  <form enctype="multipart/form-data" id="oForm">
    <input type="file" name="file" id="file" onchange="readAsDataURL()" />
    <input type="button" value="提交" onclick="doUpload()" />
  </form>
  <p>
    <img alt="" id="img"/>
  </p>
Copy after login

javascript code

FormData:

The FormData object can be used to assemble a set of key/value pairs for sending requests using XMLHttpRequest. It makes sending form data more flexible and convenient because it can be used independently of the form. If you set the form's encoding type to multipart/form-data, the data format transmitted through FormData is the same as the data format transmitted by the form through the submit() method.

Here the FormData object is used to obtain all the input data in the form, and then use an ajax request to send the data to the specified url, so that there will be no jump when the form is submitted.


  function doUpload() { 
     var formData = new FormData($( "#oForm" )[0]); 
     console.log(formData); 
     $.ajax({ 
       url: &#39;pp&#39;, 
       type: &#39;POST&#39;, 
       data: formData, 
       async: false, 
       cache: false, 
       contentType: false, 
       processData: false, 
       success: function (returndata) { 
         console.log(returndata); 
       }, 
       error: function (returndata) { 
         console.log(returndata); 
       } 
     }); 
  }
Copy after login

FileReader:

The FileReader object allows web applications to asynchronously read files stored in The contents of a file (or raw data buffer) on the user's computer, using a File or Blob object to specify the file or data to be read.

Here the FileReader object is used to obtain the image from file and convert the image into Data URL form to display in the pre-created container.


function readAsDataURL(){
  //检验是否为图像文件
    var file = document.getElementById("file").files[0];
    if(!/image\/\w+/.test(file.type)){
      alert("看清楚,这个需要图片!");

      return false;
    }else{
    var reader = new FileReader();
    //将文件以Data URL形式读入页面
    reader.readAsDataURL(file);
    reader.onload=function(e){
      var result=document.getElementById("img");
      //显示文件
      result.src= this.result ;
    }
  }
}
Copy after login

The above is the detailed content of JavaScript implements uploading and previewing images without refreshing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template