推迟文件上传表单提交,直到经过验证
可能的重复:
暂停表单提交以进行验证
这是 这篇文章。所以基本上我是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这是一个有点烦人的解决方案,但这就是使用 Javascript 的乐趣。
让浏览器和服务器之间的通信完全由 Javascript 决定,而不是由 HTML 决定。含义:
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:
form.submit();
Hope this helps
看,您不必使用
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.