0

您好,我目前正在开发一个 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]"呢?

提前致谢!

4

1 回答 1

0

尝试

function uploadFile(e) {
  e.preventDefault();
  // here the rest of the logic...
}
于 2013-11-06T20:09:17.983 回答