【问题标题】:How to submit the file on the same page without reloading [duplicate]如何在不重新加载的情况下在同一页面上提交文件[重复]
【发布时间】:2016-12-27 11:06:54
【问题描述】:

我试图找到问题“如何在不重新加载选项卡的情况下提交文件”的答案,我来到这个线程 Uploading file in a form without page refresh 在这个线程中,最佳答案是“使用隐藏的 iframe”。但是如果我们使用隐藏的 iframe,可能会在不刷新页面的情况下将表单数据提交到另一个文件。但是如何在不重新加载的情况下将文件(使用 iframe)提交到当前文件(页面)。我正在使用

<form action="<?php echo $_SERVER["PHP_SELF"];?> enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit">
</form>

我认为在我的情况下,我们甚至不能使用seralize() 方法。所以我们如何在同一页面上提交文件而不重新加载它。我认为 iframe 可能不是解决方案。

【问题讨论】:

    标签: javascript php jquery html iframe


    【解决方案1】:

    在这里我使用了这样的 jquery.. 你必须创建 FormData

    的对象
    jQuery("body").on("beforeSubmit", "form#YOUR_FORM_ID", function() {
                var form = jQuery(this);
                jQuery.ajax({
                    url: form.attr("action"),
                    type: "POST",
                    data: new FormData(this),
                    processData: false,
                    contentType: false,
                    async: false,
                    success: function(response) {
                        if (response) {
                          //your response 
                        }
                    },
                    error: function() {
                        console.log("internal server error");
                    }
                });
                return false;
            });
    

    【讨论】:

      猜你喜欢
      • 2016-12-05
      • 1970-01-01
      • 2016-07-13
      • 2016-01-20
      • 2021-09-11
      • 2014-08-10
      • 1970-01-01
      • 2012-02-23
      相关资源
      最近更新 更多