Home > Web Front-end > JS Tutorial > Introduction to the method of uploading files without refreshing in JavaScript

Introduction to the method of uploading files without refreshing in JavaScript

巴扎黑
Release: 2017-08-18 10:06:50
Original
1762 people have browsed it

This article mainly introduces the method of uploading files using JavaScript without refreshing the page. It uses js+css code to introduce the operation process in detail. Friends who need it can refer to it

Using js to give an upload A solution that does not require refreshing the page when making a file


 <input id="upload" type="file"/>
 <button id="upload-btn">upload</button>
 document.getElementById(&#39;upload-btn&#39;).onclick = function(){ 
  var oInput = document.getElementById(&#39;upload&#39;); 
  var file = oInput.files[0];  //选取文件
  var formData = new FormData(); //创建表单数据对象
  formData.append(&#39;file&#39;,file); //将文件添加到表单对象中
  fetch({       //传输
   url:&#39;./&#39;,
   mothod:&#39;POST&#39;,
   body:formData 
  }) 
  .then((d)=>{
  console.log(&#39;result is&#39;,d);
  alert("上传完毕!")
  })
 }
Copy after login

Achieve such an effect:

Use HTML+CSS to achieve the following effect: Layout, border-width: 5px, grid size is 50px*50px. When hovering, the border turns red, so semantics need to be considered.

       


 table{
   border-collapse:collapse; /* 为表格设置合并边框模型 */
   margin:50px;
   text-align:center; /* 设置文字居中 */
  } 
  table tr{
   border:none;
  } 
  table.tab td{
   width:50px;
   height:50px;
   border:5px inset blue;
  } 
  table.tab td:hover{
   border:5px solid red;
   cursor: pointer;
  }
  <table class="tab">
  <tr>
   <td>1</td>
   <td>2</td>
   <td>3</td>
  </tr>
  <tr>
   <td>4</td>
   <td>5</td>
   <td>6</td>
  </tr>
  <tr>
   <td>7</td>
   <td>8</td>
   <td>9</td>
  </tr>
  </table>
Copy after login

The above is the detailed content of Introduction to the method of uploading files without refreshing in JavaScript. 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