javascript - How to submit a form containing image files beforeSubmit?

WBOY
Release: 2016-08-30 09:36:42
Original
1334 people have browsed it

How to submit a form containing image files beforeSubmit?

Reply content:

How to submit a form containing image files beforeSubmit?

formData can upload files.

formData submission, change the form id, url and other data by yourself

<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>
Copy after login

html code

<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>
Copy after login

php code

<code>var_dump($_REQUEST,$_FILES);die;</code>
Copy after login

Pictures

javascript - How to submit a form containing image files beforeSubmit?

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!