当我使用 jQuery UI 的 Modal 形式时,为什么无法将选定的文件发布到服务器客户端?

你好CW

以下代码基于 jQuery Modal 表单,就像https://jqueryui.com/dialog/#modal-form一样

我希望单击一个按钮打开一个模态表单,并在模态表单中选择文件,然后单击“上传”按钮将数据发布到服务器端。

但是我发现当我点击“上传”按钮时没有发布数据。为什么 ?

而且,orm = dialog.find("form").on("submit", function (event) {...} ?我认为我可以删除代码的代码是什么操作form = dialog.find("form").on("submit", function (event) {...},对吗?

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Dialog - Modal form</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">

  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
      $(function () {
          var dialog, form;


          function mySubmit() {
              var valid = true;
              if (valid) {
                  dialog.dialog("close");
                  $("#MyUploadFile").submit();               

              }
              return valid;
          }

          dialog = $("#dialog-form").dialog({
              autoOpen: false,
              height: 400,
              width: 350,
              modal: true,
              buttons: {
                  "Upload": mySubmit,
                  Cancel: function () {
                      dialog.dialog("close");
                  }
              },
              close: function () {
                  form[0].reset();
              }
          });

          form = dialog.find("form").on("submit", function (event) {
              event.preventDefault();
              mySubmit();
          });

          $("#create-user").button().on("click", function () {
              dialog.dialog("open");
          });
      });
  </script>
</head>
<body>


<div id="dialog-form" title="Create new user">
  <p class="validateTips">All form fields are required.</p>

     <form action="" method="post" enctype="multipart/form-data" id="MyUploadFile">               
          <input type="file" name="myupload" multiple="multiple" />           
     </form>                                       

</div>



<button id="create-user">select files and upload</button>


</body>
</html>
曲折的

扩展您参考的示例以及我在评论中所说的内容,我确实有一个更好的示例供您考虑。

HTML

<div id="dialog-form" title="File Upload">
  <p class="validateTips">Select a file to upload.</p>
  <form>
    <fieldset>
      <label for="name">File</label>
      <input type="file" id="uploadFile" class="text ui-widget-content ui-corner-all">
      <!-- Allow form submission with keyboard without duplicating the dialog button -->
      <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>

<div id="users-contain" class="ui-widget">
  <h1>Uploaded Files:</h1>
  <table id="users" class="ui-widget ui-widget-content">
    <thead>
      <tr class="ui-widget-header ">
        <th>File</th>
        <th>Folder</th>
        <th>Date</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Archive.xls</td>
        <td>/upload</td>
        <td>02/28/2017</td>
      </tr>
    </tbody>
  </table>
</div>
<button id="upload-file-button">Upload New File</button>

的CSS

label,
input {
  display: block;
}

input.text {
  margin-bottom: 12px;
  width: 95%;
  padding: .4em;
}

fieldset {
  padding: 0;
  border: 0;
  margin-top: 25px;
}

h1 {
  font-size: 1.2em;
  margin: .6em 0;
}

div#users-contain {
  width: 350px;
  margin: 20px 0;
}

div#users-contain table {
  margin: 1em 0;
  border-collapse: collapse;
  width: 100%;
}

div#users-contain table td,
div#users-contain table th {
  border: 1px solid #eee;
  padding: .6em 10px;
  text-align: left;
}

.ui-dialog .ui-state-error {
  padding: .3em;
}

.validateTips {
  border: 1px solid transparent;
  padding: 0.3em;
}

JavaScript

$(function() {
  var dialog, form;

  function updateTips(t) {
    $(".validateTips")
      .text(t)
      .addClass("ui-state-highlight");
    setTimeout(function() {
      $(".validateTips").removeClass("ui-state-highlight", 1500);
    }, 500);
  }

  function uploadFile() {
    var valid = false;
    var $input = $("#uploadFile");
    if ($input[0].files.length === 0) {
      updateTips("You must select a file.");
      return valid;
    }
    var fileData = new FormData();
    $.each($input[0].files, function(k, v) {
      fileData.append(k, v);
    });
    // Barrowed from https://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax
    $.ajax({
      url: "./php/upload.php",
      type: "POST",
      data: fileData,
      cache: false,
      dataType: "JSON",
      processData: false,
      contentType: false,
      success: function(results, textStatus, jqXHR) {
        // 'results' will be a JSON object return from our form handler
        // 'results.error' may contain error details, like: Path Not Found
        if (typeof results.error === 'undefined') {
          // At this point, we should have uploaded the file
          // our form handler has return some response
          // We can update a table to do something with the data
          valid = true;
        } else {
          // Handle errors here
          updateTips("Error: " + results.error);
        }
      },
      error: function(jqXHR, textStatus, errorThrown) {
        // Handle errors here
        updateTips("Error: " + textStatus);
        // STOP LOADING SPINNER
      }
    });
    return valid;
  }

  dialog = $("#dialog-form").dialog({
    autoOpen: false,
    height: 400,
    width: 350,
    modal: true,
    buttons: {
      "Upload": uploadFile,
      Cancel: function() {
        dialog.dialog("close");
      }
    },
    close: function() {
      form[0].reset();
    }
  });

  form = dialog.find("form").on("submit", function(event) {
    event.preventDefault();
    uploadFile();
  });

  $("#upload-file-button").button().on("click", function() {
    dialog.dialog("open");
  });
});

如您所见,这与原始示例的工作方式非常相似。它没有通过,因为您有更多的工作需要在服务器端完成。

请记住,jQuery 是 JavaScript 的框架,也就是客户端脚本。要处理文件上传,您的 Web 服务器必须能够以某种方式处理 HTTP POST 数据。这可能是 CGI、ASP、ASP.NET 或 PHP 脚本。

我发表了关于https://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax的评论,这是一篇关于使用 AJAX 将文件上传到 PHP 脚本的非常直接的文章。我建议您通读一遍并继续研究,以便更好地理解此操作。既然您允许用户上传内容,那么您就可以通过后门脚本使您的网站和整个服务器对攻击开放。

这将使您走上正确的道路,但是旅程并没有就此结束。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用JQuery UI将Modal Box放入DataTable内

来自分类Dev

使用jQuery UI Modal Form永久更改HTML文本

来自分类Dev

当我使用附加时,jQuery将无法正常工作

来自分类Dev

当我使用 jQuery 代码时,我的导航栏消失了

来自分类Dev

如何使用“ overflow-y:auto”设置元素,但是当我使用可排序的jQuery ui水平拖动项目时仍允许浏览器滚动?

来自分类Dev

当我的客户端连接时,简单的RPC服务器将无法应答

来自分类Dev

在 ui.bootstrap Angular Modal 中使用 JQuery $.each 运算符将 CSS 应用于每个元素

来自分类Dev

当我动态生成时,jQuery UI Slider无法获取该值

来自分类Dev

当我使用jQuery验证远程时很奇怪的情况

来自分类Dev

当我离开并返回时,ui视图内的jquery不会启动。AngularJs1.6.1

来自分类Dev

当我使用jquery.ajax到PHP脚本中发布时,为什么整数变成字符串

来自分类Dev

当我使用jquery.ajax到PHP脚本中发布时,为什么整数变成字符串

来自分类Dev

angular-ui引导$ modal服务,使用指令代替

来自分类Dev

AngularJS Modal(ui.bootstrap.modal)在打开模态(modalInstance.opened)后无法触发jQuery事件

来自分类Dev

AngularJS Modal(ui.bootstrap.modal)在打开模态(modalInstance.opened)后无法触发jQuery事件

来自分类Dev

为什么我不能使用插件和jQuery UI?

来自分类Dev

为什么在服务器端和客户端同时调用recv和发送功能时使用客户端的文件描述符?

来自分类Dev

当我使用Appcache清单请求外部资源时,为什么jQuery会引发错误?

来自分类Dev

当使用此“期望”脚本启动时,为什么gdb客户端无法与其gdb服务器通信?

来自分类Dev

将值绑定到UI Bootstrap Modal

来自分类Dev

syslog-ng-使用wildcard_file时将文件名从客户端传递到服务器

来自分类Dev

为什么我无法在MeteorJS中创建jQuery UI控件?

来自分类Dev

为什么在使用ASP.NET MVC捆绑包时,我的客户端为什么要去服务器检查文件是否被修改?

来自分类Dev

为什么在使用ASP.NET MVC捆绑包时,我的客户端为什么要去服务器检查文件是否被修改?

来自分类Dev

为什么我的Boost http客户端无法连接到python http服务器?

来自分类Dev

如何使用REST将JQuery客户端中的二进制文件发布到Java Server

来自分类Dev

当我使用Ajax请求时,为什么无法从我的快速服务器上下载JSON文件?

来自分类Dev

jQuery UI不适用于bootstrap 3 modal

来自分类Dev

jQuery UI Modal Form的AJAX responseXML为空

Related 相关文章

  1. 1

    如何使用JQuery UI将Modal Box放入DataTable内

  2. 2

    使用jQuery UI Modal Form永久更改HTML文本

  3. 3

    当我使用附加时,jQuery将无法正常工作

  4. 4

    当我使用 jQuery 代码时,我的导航栏消失了

  5. 5

    如何使用“ overflow-y:auto”设置元素,但是当我使用可排序的jQuery ui水平拖动项目时仍允许浏览器滚动?

  6. 6

    当我的客户端连接时,简单的RPC服务器将无法应答

  7. 7

    在 ui.bootstrap Angular Modal 中使用 JQuery $.each 运算符将 CSS 应用于每个元素

  8. 8

    当我动态生成时,jQuery UI Slider无法获取该值

  9. 9

    当我使用jQuery验证远程时很奇怪的情况

  10. 10

    当我离开并返回时,ui视图内的jquery不会启动。AngularJs1.6.1

  11. 11

    当我使用jquery.ajax到PHP脚本中发布时,为什么整数变成字符串

  12. 12

    当我使用jquery.ajax到PHP脚本中发布时,为什么整数变成字符串

  13. 13

    angular-ui引导$ modal服务,使用指令代替

  14. 14

    AngularJS Modal(ui.bootstrap.modal)在打开模态(modalInstance.opened)后无法触发jQuery事件

  15. 15

    AngularJS Modal(ui.bootstrap.modal)在打开模态(modalInstance.opened)后无法触发jQuery事件

  16. 16

    为什么我不能使用插件和jQuery UI?

  17. 17

    为什么在服务器端和客户端同时调用recv和发送功能时使用客户端的文件描述符?

  18. 18

    当我使用Appcache清单请求外部资源时,为什么jQuery会引发错误?

  19. 19

    当使用此“期望”脚本启动时,为什么gdb客户端无法与其gdb服务器通信?

  20. 20

    将值绑定到UI Bootstrap Modal

  21. 21

    syslog-ng-使用wildcard_file时将文件名从客户端传递到服务器

  22. 22

    为什么我无法在MeteorJS中创建jQuery UI控件?

  23. 23

    为什么在使用ASP.NET MVC捆绑包时,我的客户端为什么要去服务器检查文件是否被修改?

  24. 24

    为什么在使用ASP.NET MVC捆绑包时,我的客户端为什么要去服务器检查文件是否被修改?

  25. 25

    为什么我的Boost http客户端无法连接到python http服务器?

  26. 26

    如何使用REST将JQuery客户端中的二进制文件发布到Java Server

  27. 27

    当我使用Ajax请求时,为什么无法从我的快速服务器上下载JSON文件?

  28. 28

    jQuery UI不适用于bootstrap 3 modal

  29. 29

    jQuery UI Modal Form的AJAX responseXML为空

热门标签

归档