简体   繁体   English

Javascript 以 html 形式中断文件上传

[英]Javascript breaks file upload at html form

I have a file upload form that checks the extension of the uploaded file, it works ok until I add a javascript that shows a "LOADING...." message, which always makes the error of wrong extension comes up, even though it is an allowed one.我有一个文件上传表单来检查上传文件的扩展名,它可以正常工作,直到我添加一个显示“正在加载....”消息的 javascript,这总是会出现错误扩展名的错误,即使它是一个允许的。

form.html表单.html

<form id="loader" action="uploadfile.php" method="post" enctype="multipart/form-data" onsubmit="return loading();" />
<label>Upload file:</label>
<input type="file" name="file" id="file" />
<input name="upload" type="submit" />
<input name="action" type="hidden" value="upload" />
</form>
<script>
  function loading() {
    document.getElementById("loader").innerHTML = "<div style='color: red;'>LOADING....</div>";
  }
</script>

uploadfile.php上传文件.php

<?php
$allowedExts = array("txt", "TXT");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {echo $_FILES["file"]["error"]; }
  else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upfiles/thefile.txt");
?>

Thanks to @Teemu's answer I could achieve the expected behaviour, I just moved the id from the form element to a div and that did it, here's the new code:感谢@Teemu 的回答,我可以实现预期的行为,我只是将idform元素移动到div并做到了,这是新代码:

<form action="uploadfile.php" method="post" enctype="multipart/form-data" onsubmit="return loading();" />
<label>Upload file:</label>
<input type="file" name="file" id="file" />
<input name="upload" type="submit" />
<input name="action" type="hidden" value="upload" />
<div id="loader"></div></form>
<script>
  function loading() {
    document.getElementById("loader").innerHTML = "<div style='color: red;'>LOADING....</div>";
  }
</script>

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

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