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

Introduction to the method of uploading files without refreshing in JavaScript

巴扎黑
巴扎黑Original
2017-08-18 10:06:501749browse

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("上传完毕!")
  })
 }

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>

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!

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