AjaxFileUpload文件上传组件(php+jQuery+ajax)

简介: jQuery插件AjaxFileUpload可以实现ajax文件上传,下载地址:http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupload.

jQuery插件AjaxFileUpload可以实现ajax文件上传,下载地址:http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupload.js

主要参数说明:
1,url表示处理文件上传操作的文件路径,可以测试URL是否能在浏览器中直接访问,如上:upload.php
2,fileElementId表示文件域ID,如上:fileToUpload
3,secureuri是否启用安全提交,默认为false
4,dataType数据数据,一般选json,javascript的原生态
5,success提交成功后处理函数
6,error提交失败处理函数

需要了解相关的错误提示

1,SyntaxError: missing ; before statement错误
如果出现这个错误就需要检查url路径是否可以访问

2,SyntaxError: syntax error错误
如果出现这个错误就需要检查处理提交操作的PHP文件是否存在语法错误

3,SyntaxError: invalid property id错误
如果出现这个错误就需要检查属性ID是否存在

4,SyntaxError: missing } in XML expression错误
如果出现这个错误就需要检查文件域名称是否一致或不存在

5,其它自定义错误
大家可使用变量$error直接打印的方法检查各参数是否正确,比起上面这些无效的错误提示还是方便很多。

示例代码:

=====================upload.html=======================

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ajaxfileupload图片上传控件</title>
</head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupload.js"></script>
<script language="javascript">
  jQuery(function(){   
      $("#buttonUpload").click(function(){     
         //加载图标   
         /* $("#loading").ajaxStart(function(){
            $(this).show();
         }).ajaxComplete(function(){
            $(this).hide();
         });*/
          //上传文件
        $.ajaxFileUpload({
            url:'upload.php',//处理图片脚本
            secureuri :false,
            fileElementId :'fileToUpload',//file控件id
            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;
      }) 
  })

</script>


<body>
<input id="fileToUpload" type="file" size="20" name="fileToUpload" class="input">
<button id="buttonUpload">上传</button>
<!--<img id="loading" src="loading.jpg" style="display:none;">-->
</body>
</html>

=====================upload.php=======================

$res["error"] = "";//错误信息
$res["msg"] = "";//提示信息
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],"c:\\test.jpg")){
    $res["msg"] = "ok";
}else{
    $res["error"] = "error";
}
echo json_encode($res);
如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/
目录
相关文章
N..
|
25天前
|
XML JSON 前端开发
jQuery实现Ajax
jQuery实现Ajax
N..
16 1
|
2月前
|
JavaScript 前端开发 Java
jquery ajax+spring mvc上传文件
jquery ajax+spring mvc上传文件
|
2月前
|
PHP
从建站打拿站 -- PHP(文件上传)
从建站打拿站 -- PHP(文件上传)
12 0
原生php实现大案例(特色:不登录不能使用功能 注册 登录 文件上传 发帖 列表页 详情页 )提供sql
原生php实现大案例(特色:不登录不能使用功能 注册 登录 文件上传 发帖 列表页 详情页 )提供sql
原生php实现自定义表单(支持基本类型+file类型(单文件上传))
原生php实现自定义表单(支持基本类型+file类型(单文件上传))
|
3月前
|
前端开发 JavaScript
Jquery ajax捕获错误信息
Jquery ajax捕获错误信息
16 0
|
3月前
|
前端开发 JavaScript 关系型数据库
PHP代码合集21个邮箱2个问答23个ajax特效
PHP代码合集21个邮箱2个问答23个ajax特效
17 0