扫码关注官方订阅号
看了CI框架里的文件上传类 http://codeigniter.org.cn/user_guide/libraries/file_uploading.html 好像只能通过表单的形式上传。 请问怎么通过ajax方式上传图片?
学习是最好的投资!
你应该是想实现无刷新上传。
无刷新上传方式有 2 种: 1.iFrame 无刷新上传文件 2.使用 Flash 上传 (一个好用的 Flash 上传插件 uploadify)
说一句:Ajax 是传递字符串,而不是二进制文件。所以,不存在 Ajax 上传文件这种说法。
前几天刚做完一个CI + AJAX 上传的项目,大概是用到Formdata的方法上传了 下面是代码var fd = new FormData(); fd.append("userfile", 1); fd.append("userfile", $("#imgfile").get(0).files[0]); fd.append("uid",$('#class-author').val()); $.ajax({ url: "clazz/upload", type: "POST", processData: false, contentType: false, data:fd, success: function(d) { alert(d); window.photo_url = d; alert("上传成功"); }
var fd = new FormData(); fd.append("userfile", 1); fd.append("userfile", $("#imgfile").get(0).files[0]); fd.append("uid",$('#class-author').val()); $.ajax({ url: "clazz/upload", type: "POST", processData: false, contentType: false, data:fd, success: function(d) { alert(d); window.photo_url = d; alert("上传成功"); }
亲测可用=。=希望可以帮助你~
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
你应该是想实现无刷新上传。
无刷新上传方式有 2 种:
1.iFrame 无刷新上传文件
2.使用 Flash 上传 (一个好用的 Flash 上传插件 uploadify)
说一句:Ajax 是传递字符串,而不是二进制文件。所以,不存在 Ajax 上传文件这种说法。
前几天刚做完一个CI + AJAX 上传的项目,大概是用到Formdata的方法上传了
下面是代码
var fd = new FormData(); fd.append("userfile", 1); fd.append("userfile", $("#imgfile").get(0).files[0]); fd.append("uid",$('#class-author').val()); $.ajax({ url: "clazz/upload", type: "POST", processData: false, contentType: false, data:fd, success: function(d) { alert(d); window.photo_url = d; alert("上传成功"); }亲测可用=。=希望可以帮助你~