21,890
社区成员
发帖
与我相关
我的任务
分享
function ajaxFileUpload(){
$.ajaxFileUpload ({
url:'upfile.php',
secureuri:false,
fileElementId:'pic',
dataType: 'json',
success : function (data, status){
if(typeof(data.error) != 'undefined'){
if(data.error != ''){
alert(data.error);
}else{
alert(data.msg);
}
}
},
error: function(data, status, e){
alert(e);
}
})
return false;
}
<A class=btn_addPic><SPAN><EM>+</EM>添加图片</SPAN> <INPUT class=filePrew type=file size=3 name='pic' id='pic'></A><input type='button' value='上传' class='upbt' onclick='return ajaxFileUpload();' >
$upFilePath = "../userUploadDatas/" . $_SESSION ["userId"] . "/";
$res ["error"] = ""; // 错误信息
$res ["msg"] = "exec"; // 提示信息
if (copy ( $_FILES ['pic'] ['tmp_name'], $upFilePath . $_FILES ['pic'] ['name'] )) {
$res ["msg"] = "ok";
} else {
$res ["error"] = "error";
}
echo json_encode ( $res );
<?php
echo '{"msg":"hello"}';
试试
注意一定不要有 BOM 头
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<link href="../css/manager.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/jquery.js"></script>
<script src="../js/ajaxfileupload.js" type="text/javascript"></script>
<script type="text/javascript" src="../js/jrosion.js"></script>
<script type="text/javascript">
$(document).ready(function(){
});
function upload(){
$.ajaxFileUpload({
url: 'upfile.php', //用于文件上传的服务器端请求地址
secureuri: false, //是否需要安全协议,一般设置为false
fileElementId: 'fileToUpload', //文件上传域的ID
dataType: 'json', //返回值类型 一般设置为json
success: function (data, status) //服务器成功响应处理函数
{
alert(data.msg);
//$("#img1").attr("src", data.imgurl);
if (typeof (data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
alert(data.msg);
}
}else{
alert("upload file failure!");
}
},
error: function (data, status, e)//服务器响应失败处理函数
{
alert(e);
}
});
return true;
}
</script>
<title>网站栏目管理</title>
</head>
<body>
<!-- <form action="upfile.php" method="post" enctype="multipart/form-data"> -->
<input type="file" id="fileToUpload" size="40" name="fileToUpload">
<button type="button" id="buttonUpload" onclick="return upload();">上传</button>
<!-- </form>-->
</body>
</html>
上传文件服务端upfile.php
<?php
header("Content-type: text/html; charset=utf-8");
$upFilePath = "../userUploadDatas/". $_SESSION ["userId"]."/";
$res ["error"] = ""; // 错误信息
$res ["msg"] = ""; // 提示信息
if (@move_uploaded_file(@$_FILES ["fileToUpload"] ["tmp_name"], $upFilePath .@$_FILES ["fileToUpload"] ["name"])) {
$res ["msg"] = "ok";
} else {
$res ["error"] = "error";
}
echo json_encode ( $res );
死活不能AJAX上传啊, 直接
<form action="upfile.php" method="post" enctype="multipart/form-data"> -->
<input type="file" id="fileToUpload" size="40" name="fileToUpload">
<button type="button" id="buttonUpload" onclick="return upload();">上传</button>
</form>
文件是可以上传的,我就不清楚了,我上面的代码怎么写才能AJAX上传文件,郁闷几天了,,不知道原因。。