我已经让JS运行了一个php上传表格。
每次我上传文件来测试这个文件都要上传,但进度条不起作用,我得到了一个“上传中止”文件。错误信息。
以下是JS
function _(el) {
return document.getElementById(el);
}
function uploadfile() {
var file = _("file").files[0];
var formdata = new FormData();
formdata.append("file", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHander, false);
ajax.open("POST", "uploader.php");
ajax.send(formdata);
}
function progressHandler(event) {
var percent = (event.loaded / event.total) * 100;
_("progressbar").value = (percent);
}
function completeHandler(event) {
_("status").innerHTML = "Upload Success";
_("progressbar").value = 0;
}
function errorHandler(event) {
_("status").innerHTML = "Upload Failed";
}
function abortHander(event) {
_("status").innerHTML = "Upload Aborted";
}
此脚本附加到HMTL5 <progress>
,默认值为0,最大值为100.
这是我将表单链接到的php文件:
<?php
$email = $_POST['email'];
$name = $_POST['name'];
$spc = 'test@email.com';
$message = $_POST['message'];
$size = $_FILES['file']['size'];
$file = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
$filetype = $_FILES['file']['type'];
$link = 'http://www.spcink.com/uploads/' . $email . '%7c' . $name;
if(!empty($file)) {
mkdir('../uploads/' . $email . '|' . $name, 0777, true);
move_uploaded_file($file, '../uploads/' . '/'. $email . '|' . $name . '/' . $filename);
mail($spc, 'File upload from:' . $name, $message . $link);
}
?>
我似乎无法弄清楚这里有什么问题。有人可以看看这个并帮助我!谢谢!