如何在XMLHttpRequest中获取实际文件?
16lz
2021-01-22
How can i get the actual file in an XMLHttpRequest? Using evt.target.files returns a fileList object, and I am not able to grab the actual file on the php end, in order to save on server.
如何在XMLHttpRequest中获取实际文件?使用evt.target.files返回一个fileList对象,我无法抓取php端的实际文件,以便保存在服务器上。
Below is the code, and I have attached an image showing the responseText returned from the php code print_r($_POST);
下面是代码,我附上了一个显示从php代码print_r($ _ POST)返回的responseText的图像;
function handleFileSelect(evt) {
reader = new FileReader();
reader.onload = function(e) {
// Ensure that the progress bar displays 100% at the end.
progress.style.width = '100%';
progress.textContent = '100%';
setTimeout("document.getElementById('progress_bar').className='';", 2000);
///////////THE JSON/////////////to encase file in JSON object
var sendFile = function(url, successHandler, errorHandler) {
var xhr = typeof XMLHttpRequest != 'undefined' ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('post', url, true);
//XHR progress tracking
xhr.upload.onprogress = function(event) {
//var native_progress = document.getElementById("native_progress");
//alert('in native_progress function');
if (event.lengthComputable) {
var complete = (event.loaded / event.total * 100 | 0);
native_progress.value = native_progress.innerHTML = complete;
}
};
xhr.onreadystatechange = function() {
var status;
var data;
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate
if (xhr.readyState == 4) { // `DONE`
status = xhr.status;
if (status == 200) {
//data = JSON.parse(xhr.responseText);
data = xhr.responseText;
successHandler && successHandler(data);
} else {
errorHandler && errorHandler(status);
}
}
};
var formData = new FormData();
formData.append('file', evt.target.files[0]);
//xhr.setRequestHeader("Content-type", "multipart/gorm-data");
//xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(formData);
};
sendFile('/ePub_Reader/temp_upload/save.php', function(data) {
alert('Response Text: ' + data);
}, function(status) {
alert('Something went wrong.');
});
///////////THE JSON - end/////////////
// Read in the image file as a binary string.
//reader.readAsBinaryString(evt.target.files[0]);
reader.readAsDataURL(evt.target.files[0]);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
更多相关文章
- 【MySQL 技巧分享】 mysql -e 加 v 简化代码
- Oracle相当于MySQL代码“插入虚拟”以返回错误消息
- 登录使用PHP并´t显示任何html代码
- MySql数据库——文件
- mysql日志文件过大导致磁盘空间不够的问题
- PHP读取Excel文件的内容并写入Mysql数据库
- 如何创建a '。sql的文件
- MySQL(Navicat)运行.sql文件时报错:[Err] 2006
- 有没有一种方法可以在不破坏外键依赖关系的情况下将MySQL数据库