推迟文件上传表单提交,直到经过验证

发布于 2024-10-03 09:46:36 字数 3822 浏览 2 评论 0原文

可能的重复:
暂停表单提交以进行验证

这是 这篇文章。所以基本上我是在 iframe 中上传文件。但在提交之前,我从表单中获取数据并使用 django 的内置系统检查其有效性(目前它只是一个接受 foo: bar 并返回 json result = "true" 的虚拟函数)。我使用两个按钮 - 假可见,调用验证,第二个按钮 - 隐藏,提交表单。使用下面的代码,我可以执行验证,然后当结果为肯定时提交表单。现在,如果我对表单的目标进行硬编码,则不会显示我的警报,并且我非常确定上传不会执行(不幸的是,上传列表每 8 小时刷新一次,所以我会知道它是否在一段时间内有效)。如果未指定目标,则文件将通过重定向上传,因此整个“提交”事件侦听器将被省略。更重要的是,整个代码在 Chrome 中根本不起作用。从接收验证响应到显示它,大约需要 2-3 秒(查看 firebug),这是我以前从未见过的。我真的很想让它发挥作用,因为我已经浪费了两天时间却没有任何结果。

示例链接:

http://ntt.vipserv.org/artifact/

JS:

<script>  
    $('#fake_upload_submit').click(function(e){
        e.preventDefault();

        var fileUploadForm = document.getElementById('file_upload_form');
        fileUploadForm.addEventListener("submit", function() {
            alert("sent in iframe");
            fileUploadForm.target = 'upload_target';
        }, false);       

        $.ajax({
            type: "POST",
            url: '/artifact/upload/check-form/',
            data: 'foo=bar',
            dataType: "json",
            success: function(data){
                if(data['result'] == "true"){
                    $("#message").show();
                    $("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
                    setTimeout(function(){
                        $("#message").fadeOut("slow", function () {
                            $("#message").hide();
                        });
                    }, 1500);

                    fileUploadForm.submit();

                }
                else{
                    $("#message").show();
                    $("#message").fadeIn(400).html('<span">Response false</span>');
                    setTimeout(function(){
                        $("#message").fadeOut("slow", function () {
                            $("#message").hide();
                        });
                    }, 1500);                    
                    return false;
                }
            }
        });
        return false;        
    });   
</script>

html:

<div id="message" style="background:black; width:400px; height:80px; display:none; color:white;">
</div>
<h1>Submit</h1>
<form action="{{upload_url}}" method="POST" target="" id="file_upload_form" enctype="multipart/form-data">
    {% render_upload_data upload_data %}
    <table>{{ form }}</table>    
    <p>
        <input type="hidden" maxlength="64" name="myfileid" value="{{ myfileid }}" >
    </p>
    <p>
        <input type="submit" style="display:none" id="true_upload_submit" value="Submit" />
    </p>    
    <iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
    <p>
        <input type="submit" id="fake_upload_submit" value="Submit" />
    </p>

编辑:

IE 的调试器显示 Object does not support this property or method for:

    fileUploadForm.addEventListener("submit", function() {
        alert("sent in iframe");
        fileUploadForm.target = 'upload_target';
    }, false);

因为使用了 addEventListener,但没有更多错误。萤火虫很干净。在 Chrome 中:

检查表单中“无法加载资源”。这很有趣,因为 .../check-form/ 的结果是:

{
message: "Response = True"
result: "true"
}

所以对我来说看起来很好。还尝试了 data.message 而不是 data["message"] 但没有任何变化。

Possible Duplicate:
Pause form submission for validation

It's a continuation of this post. So basically I'm uploading a file in an iframe. But before submitting, I grab data from the form and using django's built-in system check their validity (currently it's just a dummy function that takes foo: bar, and returns json result = "true" ). I use two buttons - fake visible, that calls validation and second - hidden, that submits the form. With the code below I'm able to perform validation, then when it's result is positive form is submitted. Now if I hardcode target for form, my alert is not shown and I'm pretty sure that the upload is not performed (unfortunately list of uploads is refreshed each 8h so I'll know if it worked in some time). If the target is not specified, file is uploaded with redirect so the whole 'submit' event listener is omitted. What more, tho whole code doesn't work at all in Chrome. And it takes like 2-3s (looking at firebug) between receiving response from validation, and displaying it which I've never seen before. I'm really desperate on making it work, since I've already wasted 2 days and without any results.

Sample link:

http://ntt.vipserv.org/artifact/

JS:

<script>  
    $('#fake_upload_submit').click(function(e){
        e.preventDefault();

        var fileUploadForm = document.getElementById('file_upload_form');
        fileUploadForm.addEventListener("submit", function() {
            alert("sent in iframe");
            fileUploadForm.target = 'upload_target';
        }, false);       

        $.ajax({
            type: "POST",
            url: '/artifact/upload/check-form/',
            data: 'foo=bar',
            dataType: "json",
            success: function(data){
                if(data['result'] == "true"){
                    $("#message").show();
                    $("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
                    setTimeout(function(){
                        $("#message").fadeOut("slow", function () {
                            $("#message").hide();
                        });
                    }, 1500);

                    fileUploadForm.submit();

                }
                else{
                    $("#message").show();
                    $("#message").fadeIn(400).html('<span">Response false</span>');
                    setTimeout(function(){
                        $("#message").fadeOut("slow", function () {
                            $("#message").hide();
                        });
                    }, 1500);                    
                    return false;
                }
            }
        });
        return false;        
    });   
</script>

html:

<div id="message" style="background:black; width:400px; height:80px; display:none; color:white;">
</div>
<h1>Submit</h1>
<form action="{{upload_url}}" method="POST" target="" id="file_upload_form" enctype="multipart/form-data">
    {% render_upload_data upload_data %}
    <table>{{ form }}</table>    
    <p>
        <input type="hidden" maxlength="64" name="myfileid" value="{{ myfileid }}" >
    </p>
    <p>
        <input type="submit" style="display:none" id="true_upload_submit" value="Submit" />
    </p>    
    <iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
    <p>
        <input type="submit" id="fake_upload_submit" value="Submit" />
    </p>

EDIT:

IE's debugger shows Object doesn't support this property or method for:

    fileUploadForm.addEventListener("submit", function() {
        alert("sent in iframe");
        fileUploadForm.target = 'upload_target';
    }, false);

because of using addEventListener, but no more errors. Firebug is clean. In Chrome :

"Failed to load resource" at check-form. This is interesting, since the result at .../check-form/ is :

{
message: "Response = True"
result: "true"
}

so looks fine to me. Tried also data.message instead of data["message"] but nothing changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2024-10-10 09:46:36

也许这是一个有点烦人的解决方案,但这就是使用 Javascript 的乐趣。
让浏览器和服务器之间的通信完全由 Javascript 决定,而不是由 HTML 决定。含义:

  • 当您编写 HTML 时,请忽略操作;将提交按钮更改为常规按钮。
  • 通过 ajax 运行验证(就像现在所做的那样),如果验证成功,则调用一个新函数来管理表单提交。
  • 这个表单提交函数将显示您想要的消息,然后将操作添加到表单中,并运行 form.submit();

希望这有帮助

Maybe a little bit of an annoying solution, but that's the joy of working with Javascript.
Leave the communication between browser and server completely up to Javascript, and not to HTML. Meaning:

  • When you write your HTML, leave out the action; change the submit buttons to regular buttons.
  • Run validation via ajax (as is done now), and if the validation is successful call a new function which will manage the form submition.
  • This form-submitting function will display the message you wanted, and after that will add the action to the form, and will run form.submit();

Hope this helps

哆兒滾 2024-10-10 09:46:36

看,您不必使用 fileUploadForm.addEventListener("submit", function (){...}, false)。这正是 $(fileUploadForm).submit(function (){...}) 所做的,并且它具有跨所有浏览器工作的优点。

Look, you don't have to use fileUploadForm.addEventListener("submit", function (){...}, false). This is exactly what $(fileUploadForm).submit(function (){...}) does, and it has the advantage of working across all browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文