我使用FormData jquery上传图片,但图片不在uploads文件夹中。我想我没有以正确的方式关注FormData。我的Firebug控制台和JS如下所示。请检查代码并纠正我。
Firebug控制台帖子
来源 ----------------------------- 55572121128376 内容处理:表格数据; NAME = “图像”
标题= 2及字幕= 2及圆= 2及文本=安培;价格= 2及pseudo_price = 2及链接= 2及颜色=#8b8b8b&安培;柱= 2及addForm = addForm 和ID = 70 ----------------------------- 55572121128376-- 表格
<input name="image" type="file" id="file" >
JS
function saveData(id){
var ID = id;
var title = $("#title_"+ID).val();
var subtitle = $("#subtitle_"+ID).val();
var circle = $("#circle_"+ID).val();
var text = $("#text_"+ID).val();
var price = $("#price_"+ID).val();
var pseudo_price = $("#pseudoprice_"+ID).val();
var link = $("#link_"+ID).val();
var color = $("#color_"+ID).val();
var column = $("#column_"+ID).val();
var addForm = 'addForm';
var dataString = 'title='+title+'&subtitle='+subtitle+'&circle='+circle+'&text='+text+'&price='+price+'&pseudo_price='+pseudo_price+'&link='+link+'&color='+color+'&column='+column+'&addForm='+addForm+'&ID='+ID;
var formdata = new FormData();
formdata.append('image', dataString);
$.ajax({
type: "post",
url: "insert.php",
data: formdata,
cache: false,
processData: false,
contentType: false,
success: function(response) {
alert(response);
}
});
};
insert.php
if (isset($_FILES['image'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif"
) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
echo "The file " . basename($_FILES["image"]["name"]) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
答案 0 :(得分:0)
您需要使用此类表单数据
var form = $('form')[0]; // You need to use standart javascript object here
var formData = new FormData(form);
formData.append('image', dataString);
// now rest of your code
...