使用jquery、ajax和json上传PHP文件
我正在尝试将文件上载到服务器,但无法上载,因为文件名包含C:\fakepath\untitled1.doc 下面是我的jquery使用jquery、ajax和json上传PHP文件,php,json,ajax,Php,Json,Ajax,我正在尝试将文件上载到服务器,但无法上载,因为文件名包含C:\fakepath\untitled1.doc 下面是我的jquery $(document).ready(function() { $("#save").click(function() { var id = $("#id").val(); //alert(id); if(id =="") {
$(document).ready(function()
{
$("#save").click(function()
{
var id = $("#id").val();
//alert(id);
if(id =="")
{
var method = "Add";
id = "";
}
else
{
var method = "Edit";
id = id;
}
var title = $("#title").val();
//alert(title);
var fname = $("#fname").val();
var lname = $("#lname").val();
var day = $("#day").val();
var month = $("#month").val();
var year = $("#year").val();
var city = $("#city").val();
var addr = $("#addr").val();
var tel = $("#telno").val();
var email = $("#email").val();
var exp = $("#exp").val();
var reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
var filename = $("#resume").val();
if(Validate(title,fname,lname,day,month,year,city,addr,tel,email,exp,reg,filename))
{
$.ajax(
{
url : "addResumeAjax.php",
type : "POST",
dataType : "json",
data : {
method : method,
id : id,
title : title,
fname : fname,
lname : lname,
day : day,
month : month,
year : year,
city : city,
addr : addr,
tel : tel,
email : email,
exp : exp,
filename: filename
},
ContentType : "aplication/json",
success : function(response)
{
$("#msg").html(response.message);
cityBind(response.citydata,response.city);
},
error : function(err)
{
alert(JSON.stringify(err));
}
}
)
}
}
);
}
);
请告诉我哪里出错。如果有任何帮助,我们将不胜感激。试试以下方法:
JS:
Php:
这对我来说很好。检查此链接,我希望它能帮助您
在jQuery中使用serialize函数。它将把你的所有数据带到你的php页面。并确保在表单标记中声明了enctype=multipart/formdata。在使用serialize函数后,它会将除filename之外的所有数据传输到php文件。我使用alert Box检查了数据。它引发了一个类似myFileJqry.js:134 Uncaught TypeError:formdata.append的异常。这不是一个函数。我可以使用上述代码上载文件,但找不到其他表单字段数据,请您也帮我一下,好吗?…提前谢谢。使用$_REQUEST['field\u name']获取任何字段的值并使用它。未捕获引用错误:未定义文件您正在使用哪个版本的jquery?你能给我看看你的HTML文件吗?jQuery V1.110.C C 2005, 2014 jQuery基金会,JQuyr.Org/LixEnSee,我可以粘贴我的HTML,我是这个网站的新手,我不知道很多选项。
$('#upload').on('click', function() {
var file_data = $('#pic').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data); // append more data in the same way
$.ajax({
url : 'upload.php', // point to server-side PHP script
dataType : 'text', // what to expect back from the PHP script, if anything
cache : false,
contentType : false,
processData : false,
data : form_data,
type : 'post',
success : function(output){
alert(output); // display response from the PHP script, if any
}
});
$('#pic').val(''); /* Clear the file container */
});
<?php
if ( $_FILES['file']['error'] > 0 ){
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']))
{
echo "File Uploaded Successfully";
}
}
?>