简体   繁体   English

Javascript和AJAX发布方法

[英]Javascript and AJAX post method

I am currently working on one upload progress bar with HTML5 and Javascript/Ajax. 我目前正在使用HTML5和Javascript / Ajax开发一个上传进度栏。

So here is my Javascript code: 所以这是我的Javascript代码:

function _(el){
    return document.getElementById(el);
}
// Submit the upload form
function uploadFiles(){

    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);
    return false;

    if(checkFileNameFormat()){ return false; }
    if(checkDisallowFileExtensions()){ return false; }
    if(checkAllowFileExtensions()){ return false; }
    if(checkNullFileCount()){ return false; }
    if(checkDuplicateFileCount()){ return false; }

    var total_uploads = 0;

    for(var i = 0; i < upload_range; i++){
        if(document.form_upload.elements['upfile_' + i].value != ""){ total_uploads++; }
    }

    document.getElementById('total_uploads').innerHTML = total_uploads;
    document.form_upload.submit();
    document.getElementById('upload_button').disabled = true;


    for(var i = 0; i < upload_range; i++){ document.form_upload.elements['upfile_' + i].disabled = true; }  
}
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";
}

In this case the form is NOT submithing data to action pointer action="[var.path_to_upload_script] from the form element BUT the Upload Progress bar is working. 在这种情况下,表单不会将数据提交到表单元素中的动作指针action="[var.path_to_upload_script] ,但是上载进度栏action="[var.path_to_upload_script]工作。

When I remove return false; 当我删除return false; from the Javascript the upload progress bar is not showing, but the HTML form is submitting data to the script referred to its action attribute. 在Javascript中,上传进度栏未显示,但HTML表单正在将数据提交到引用其action属性的脚本。

So let me show you the code of my HTML form: 因此,让我向您展示我的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>
      <h3 id="status"></h3>
      <p id="loaded_n_total"></p>
    <noscript><br><input type="reset" name="no_script_reset" value="Reset" />&nbsp;&nbsp;&nbsp;<input type="submit" name="no_script_submit" value="Upload" /></noscript>
    </form>
    <br>
    <script language="javascript" type="text/javascript">
        document.writeln('<input type="button" name="reset_button" value="Reset" onClick="resetForm();">&nbsp;&nbsp;&nbsp;<input type="button" id="upload_button" name="upload_button" value="Upload" onClick="uploadFiles();">');
    </script>

My questions are: 我的问题是:

  • where is my mistake in this Javascript 我在这个Javascript中的错误在哪里
  • how i can make it post the data to [var.path_to_upload_script] and show up the upload progress bar? 我怎样才能将数据发布到[var.path_to_upload_script]并显示上传进度栏?

On other hand when I test this code in Opera it works, but it Google Chrome it says Upload Aborted ... 另一方面,当我在Opera中测试此代码时,它可以运行,但是在Google Chrome浏览器中,它显示Upload Aborted ...

Reason Data Is Not Submitted 原因数据未提交

Currently, the placement of return false; 目前, return false;的位置为return false; is causing the page to exit the function before document.form_upload.submit(); 导致页面退出document.form_upload.submit();之前的功能document.form_upload.submit(); executes. 执行。 That is why the data is not submitted. 这就是为什么不提交数据的原因。

Notes on Return False 假通知书

You may need to put a return false; 您可能需要将return false;设为return false; at the end of the function, to maintain consistency (if that is expected by some code that is calling the function and/or if it is tied into whether or not the page is submitted). 在函数的末尾,以保持一致性(如果正在调用该函数的某些代码期望这样做和/或是否与是否提交该页面相关联)。

There may also be some other action in the function that is causing the progress bar to fail. 该功能中还可能有其他动作导致进度条失败。 You can experiment with moving it down in the code until you see the progress bar fail -- That will reveal what line is messing up the progress bar, which will be the first step on solving the problem. 您可以尝试在代码中向下移动它,直到看到进度条失败为止-这将显示出什么行使进度条弄乱了,这将是解决问题的第一步。

My Expected Solution 我期望的解决方案

My guess as to the reason you do not see the progress bar at all when you remove return false; 我的猜测是,删除return false;根本看不到进度栏,原因是return false; is that it is precisely because of the data being submitted. 正是因为提交了数据。 When document.form_upload.submit(); document.form_upload.submit(); executes, the page will submit, which would result in either a page reload or loading of the form's action page. 执行后,页面将提交,这将导致页面重新加载或加载表单的操作页面。

The overall solution will depend a bit on how your progress bar control works, but it may be as simple as moving document.form_upload.submit(); 整体解决方案将在某种程度上取决于进度条控件的工作方式,但它可能就像移动document.form_upload.submit();一样简单document.form_upload.submit(); into the completeHandler() function, as you do not want the page to submit until after the upload is complete. 进入completeHandler()函数,因为您不希望页面在上传完成后才提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM