这次给大家带来php+ajax无刷新文件上传实现步骤详解,php+ajax无刷新文件上传实现的注意事项有哪些,下面就是实战案例,一起来看一下。
文件上传的表单格式
AjaxFileUpload实现文件异步上传效果更好,使用简单:
nbsp;html> <title></title> <meta> <script></script> <script></script> <script> 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> <input> <button>上传</button>
上传还可以传递参数:
var data = { name: 'my name', description: 'short description' } $.ajaxFileUpload({ url: 'upload.php', secureuri: false, data: data, fileElementId: 'fileToUpload', dataType: 'json', success: function (data) { alert(data.msg); }, error: function (data) { alert("error"); } });
主要参数说明:
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直接打印的方法检查各参数是否正确,比起上面这些无效的错误提示还是方便很多。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是php+ajax无刷新文件上传实现步骤详解的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号