search

Home  >  Q&A  >  body text

javascript - 请问beforeSubmit怎么提交包含图片文件的表单?

请问beforeSubmit怎么提交包含图片文件的表单?

PHP中文网PHP中文网2817 days ago1373

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-11 12:29:10

    formData可以上传文件。

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-11 12:29:10

    formData 提交,自行更换表单id ,url等数据

    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);  
              }  
         });  
    }  

    html代码

    <!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>

    php代码

    var_dump($_REQUEST,$_FILES);die;

    图片

    reply
    0
  • Cancelreply