請問beforeSubmit怎麼提交包含圖片檔案的表單?
請問beforeSubmit怎麼提交包含圖片檔案的表單?
formData可以上傳檔案。
formData 提交,自行更換表單id ,url等資料
<code>function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: 'http://localhost/file/upload' , type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { alert(returndata); }, error: function (returndata) { alert(returndata); } }); } </code>
html代碼
<code><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script> <title>Title</title> </head> <body> <form id="uploadForm"> <input name="asas"> <input name="adsfadsf"> <input type="file" name="asdfadsasaaa"> <input name="asasdfasdfasas"> <input type="button" onclick="doUpload()"> </form> </body> </html> <script> function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: './form.php' , type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { alert(returndata); }, error: function (returndata) { alert(returndata); } }); } </script></code>
php程式碼
<code>var_dump($_REQUEST,$_FILES);die;</code>
圖片