【2】經過Ajax方式上傳文件(圖片),使用FormData進行Ajax請求

  • HTML:  

<form id= "uploadForm">  ajax

    <>指定文件名: <input type="text" name="filename" value= ""/></>  async

    <>上傳文件: <input type="file" name="file"/></ p>  url

    <input type="button" value="上傳" onclick="doUpload()" />  spa

</form>  orm

  • JS:

function doUpload() {  接口

var formData = new FormData($( "#uploadForm" )[0]);  input

     $.ajax({  string

          url: 'xxx' ,                  --------------這裏是請求的接口io

          type: 'POST',              function

          data: formData,  

          async: false,  

          cache: false,  

          contentType: false,  

          processData: false,  

          success: function (returndata) {  

              alert(returndata);  

          },  

          error: function (returndata) {  

              alert(returndata);  

          }  

     });  

相關文章
相關標籤/搜索