php+ajax图片上传,及时文件上传 摆渡整合
2017-02-28 11:35
417 查看
html
<input type="file" onchange="ajaxFileUpload(this.files);" />
script
var xhr;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
}
//上传方法
function ajaxFileUpload(file) {
var fileObj = file[0];
var FileController = '../upload.php';
var form = new FormData();
form.append("myfile", fileObj);
createXMLHttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("post", FileController, true);
xhr.send(form);
xhr.onreadystatechange = function() {
if ((xhr.readyState == 4) && (xhr.status == 200)) {
console.log(xhr.responseText);
// json对象转换
var redata = JSON.parse(xhr.responseText);
console.log(redata.filepath);
} else {
alert('fail');
}
}
}
function handleStateChange() {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status == 0) {
var result = xhr.responseText;
var json = eval("(" + result + ")");
alert('图片链接:\n' + json.file);
}
}
}
有个小BUG,同一页面上传重复图片,上传没有反应。
<input type="file" onchange="ajaxFileUpload(this.files);" />
script
var xhr;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
}
//上传方法
function ajaxFileUpload(file) {
var fileObj = file[0];
var FileController = '../upload.php';
var form = new FormData();
form.append("myfile", fileObj);
createXMLHttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("post", FileController, true);
xhr.send(form);
xhr.onreadystatechange = function() {
if ((xhr.readyState == 4) && (xhr.status == 200)) {
console.log(xhr.responseText);
// json对象转换
var redata = JSON.parse(xhr.responseText);
console.log(redata.filepath);
} else {
alert('fail');
}
}
}
function handleStateChange() {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status == 0) {
var result = xhr.responseText;
var json = eval("(" + result + ")");
alert('图片链接:\n' + json.file);
}
}
}
有个小BUG,同一页面上传重复图片,上传没有反应。
相关文章推荐
- 如何实现jQuery的Ajax文件上传,PHP如实文件上传. AJAX上传文件,PHP上传文件。
- php+ajax上传图片
- Ajax+PHP+jQuery图片裁剪上传
- php+jquery+ajax无刷新图片上传裁切,模拟flash头像上传实例 推荐
- php结合jquery异步上传图片(ajaxSubmit)
- jquery + ajax +php 实现上传图片
- php ajax无刷新上传图片实例代码
- Ajax+php上传图片
- thinkphp(php)+Ajax上传图片
- jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码
- php ajax无刷新上传图片实例代码
- PHP+jquery+ajaxupload 无刷新上传文件或图片
- jquery php ajax多图片上传.上传进度,生成缩略图
- 如何实现jQuery的Ajax文件上传,PHP如实文件上传. AJAX上传文件,PHP上传文件。
- jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码
- Ajax+PHP+jQuery图片截图上传
- jQuery php ajax图片上传
- jquery php ajax多图片上传.上传进度,生成缩略图
- Ajax+PHP+jQuery图片截图上传
- PHP+jQuery+Ajax实现多图片上传效果