实际上我想通过将我的表单(包含我的上传文件)发送到php上传处理文件,使用$ .post异步上传照片。.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 :(得分: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>