HTML5和Javascript上传进度条 - 按钮与提交输入类型

时间:2013-11-06 19:20:47

标签: javascript php html5

您好我正在处理一个HTML5上传进度条。 我的剧本有一个非常奇怪的问题。 当我在我的HTML表单中使用时:

<input type="button" value="Upload File" onclick="uploadFile()">

上传进度条工作正常但不提交HTML表单数据。

当我在我的HTML中使用时:

<input type="submit" value="Upload File" onclick="uploadFile()">

它给了我错误Upload Aborted,但它正在提交表单。

所以这是我的Javascript代码:

<script>
function _(el){
    return document.getElementById(el);
}
function uploadFile(){
    var file = _("upfile_0").files[0];
    //alert(file.name+" | "+file.size+" | "+file.type);
    var formdata = new FormData();
    formdata.append("upfile_0", file);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "file_upload_parser.php");
    ajax.send(formdata);
}
function progressHandler(event){
    _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
    var percent = (event.loaded / event.total) * 100;
    _("progressBar").value = Math.round(percent);
    _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
    _("status").innerHTML = event.target.responseText;
    _("progressBar").value = 0;
}
function errorHandler(event){
    _("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
    _("status").innerHTML = "Upload Aborted";
}
</script>

以下是我的HTML:

    <form name="form_upload" method="post" enctype="multipart/form-data"  action="[var.path_to_upload_script]" style="margin: 0px; padding: 0px;">
    <noscript><input type="hidden" name="no_script" value="1" /></noscript>
    <input type="hidden" name="title" value="[var.title]" />
    <input type="hidden" name="description" value="[var.description]" />
    <input type="hidden" name="tags" value="[var.tags]" />
    <input type="hidden" name="location_recorded" value="[var.location_recorded]" />
    <input type="hidden" name="allow_comments" value="[var.allow_comments]" />
    <input type="hidden" name="allow_embedding" value="[var.allow_embedding]" />
    <input type="hidden" name="public_private" value="[var.public_private]" />
    <input type="hidden" name="channel" value="[var.channel]" />
    <input type="hidden" name="channel_name" value="[var.channel_name]" />
    <input type="hidden" name="sub_cat" value="[var.sub_cat]" />
    <input type="hidden" name="form_submitted" value="yes" />

    <div id="upload_slots"><span class="font5_16"><b>[var.lang_please_upload]</b></span><input type="file" name="upfile_0" id="upfile_0" /></div>
      <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
      <div id="status"></div>
      <div id="loaded_n_total"></div>
        <input type="submit" value="Upload File" onclick="uploadFile()">
    </form>

那么我如何让这个上传进度条工作,HTML FORM中的数据发送到action="[var.path_to_upload_script]"

提前致谢!

1 个答案:

答案 0 :(得分:0)

尝试

function uploadFile(e) {
  e.preventDefault();
  // here the rest of the logic...
}