2017-08-06 164 views
-1

其实我想用$ .post通过发送我的表单(包含我的上传文件)到一个php上传处理文件来异步上传照片。 .serializeArray()不会做必要的。有人可以建议任何方式来这样做吗?

HTML:

<form action="upload1.php" method="post" id="usrform" enctype="multipart/form-data" > 
     <div class="input-group text-center "> 
     <input type="file" class="form-control btn btn-default" name="fileToUpload" id="fileToUpload"data-toggle="tooltip" data-placement="left" title="Choose your video to upload by clicking on the choose file button"></div> 
     <br><div class="form-group"><strong><span class="glyphicon glyphicon-pencil" style="font-size:13px" ></span> Caption:</strong> <input form="usrform" name="post"id="textarea1" type="text" class="form-control" name="image" placeholder="Write something" data-toggle="tooltip" data-placement="top" title=""> 
     </div> 
     <div class="text-right"> 
     <button type="submit" id="sub"class="btn btn-primary">POST</button> 
     </div></form> 

的javascript:

$('#sub').click(function(){$.post('porthome_.php',$("#usrform").serializeArray(),function(info){ 

      clearInput();$("#myModal1 .close").click(); 

      });}); 

      $('#usrform').submit(function(){return false;}); 

PHP:

<?php 
require("../includes/config.php"); 

$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["submitfile"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     //apologize("File is an image - " . $check["mime"] . ".","portimage_.php","Error"); 
     $uploadOk = 1; 
    } else { 
     apologize("File is not an image.","portimage_.php","Error"); 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    apologize("Sorry, file already exists.Try changing the name of your image file.","portgallery_.php","Error"); 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 5000000) { 
    apologize("Sorry, your file is too large.","portimage_.php","Error"); 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    apologize("Sorry, only JPG, JPEG, PNG & GIF files are allowed.","portimage_.php","Error"); 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    apologize("Sorry, your file was not uploaded.","portimage_.php","Error"); 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 


    apologize("The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.","porthome_.php","Success"); 

    } else { 
     apologize("Sorry, there was an error uploading your file.","portimage_.php","Error"); 
    } 
}?> 

如果有任何其他的方式来做到这一点,我们将不胜感激。

+0

,你可以使用'FormData'对象 - 适用于通过AJAX发送文件 – RamRaider

+0

u能帮助我与,我不知道如何实现 –

回答

0

我不使用jQuery,所以可以提供没有真正的指导你使用的$.post()功能,但在普通的,香草的JavaScript你可以这样做。

<form action="upload1.php" method="post" id="usrform" enctype="multipart/form-data" > 
    <div class="input-group text-center "> 
     <input type="file" class="form-control btn btn-default" name="fileToUpload" id="fileToUpload"data-toggle="tooltip" data-placement="left" title="Choose your video to upload by clicking on the choose file button"> 
    </div> 
    <br> 
    <div class="form-group"> 
     <strong><span class="glyphicon glyphicon-pencil" style="font-size:13px" ></span> Caption:</strong> 
     <input form="usrform" name="post"id="textarea1" type="text" class="form-control" name="image" placeholder="Write something" data-toggle="tooltip" data-placement="top" title=""> 
    </div> 
    <div class="text-right"> 
     <button type="submit" id="sub" class="btn btn-primary">POST</button> 
    </div> 
</form> 

<script> 
    function uploadfiles(e){ 
     var data=new FormData(document.getElementById('usrform')); 
     var xhr=new XMLHttpRequest(); 
     xhr.onload=function(e){ 
      /* You probably would want to do more than popup an alert here! */ 
      alert(this.response); 
     } 
     xhr.onerror=function(e){ 
      alert(e); 
     } 
     xhr.open('POST', 'upload1.php', true); 
     xhr.send(data); 
    } 
    document.addEventListener('DOMContentLoaded',function(){ 
     document.getElementById('sub').onclick=uploadfiles; 
    },false); 
</script> 
相关问题