使用 jquery 和 ajax 提交文件元素:不需要插件!
我正在尝试通过 ajax 提交包含文件元素的表单。
<form id="classic_upload" enctype="multipart/form-data>
<input type="file" name="file" id="file"/>
<br/>
<!-- ...other inputs...-->
<button type="button" id="classic_save"> Send </button>
</form>
我需要做的是提交这个表单并检查文件是否满足一些要求,所以我为此表单编写了一个ajax提交
$('#classic_save').click(function(){
$.ajax({
type:"POST",
url:'<g:createLink action="classicUploadFile" controller="scan"/>',
success: function(msg){
alert("Data Loaded: " + msg);
}
});
});
但是,我不知道如何通过ajax发送文件。
一些上下文
最初我们使用 swfUpload 来实现此目的。然而,我们遇到了一些 https 问题和一些证书问题。所以我们决定实现一个基本的 html 后备。插件很好,但我们需要保证这个回退是防弹的(想想谷歌邮件“经典上传”)。
有什么想法吗? iframe 是可行的方法吗(阅读谷歌邮件使用它们进行经典上传的地方)
提前致谢。
I'm trying to do a submit via ajax of a form that contains a file element.
<form id="classic_upload" enctype="multipart/form-data>
<input type="file" name="file" id="file"/>
<br/>
<!-- ...other inputs...-->
<button type="button" id="classic_save"> Send </button>
</form>
What I need to do is to submit this form and check if the file fulfills some requirements, so I wrote an ajax submit for this form
$('#classic_save').click(function(){
$.ajax({
type:"POST",
url:'<g:createLink action="classicUploadFile" controller="scan"/>',
success: function(msg){
alert("Data Loaded: " + msg);
}
});
});
However, I have no idea how to send the file through ajax.
Some context
Originally we were using swfUpload for this. However, we ran into some trouble with https and some certificate issues. So we decided to implement a basic html fallback. Plugins are nice, but we need to guarantee that this fall back is bullet proof (thinking of google mail "classic upload").
Any thoughts? Are iframes the way to go (read somewhere google mail uses them for their classic upload)
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您愿意,jQuery Uploadify 插件 可以满足您除了其他出色功能之外的需求。
If you like, there is jQuery Uploadify plugin to do exactly what you are looking for other than other great features.
我在我的网站上使用 jQuery AJAX 表单插件 来 上传文件。
I use the jQuery AJAX form plugin on my site to upload files.
在这里,您将找到一个非常好的示例/教程,介绍如何上传一个文件 http://www. phpletter.com/Demo/AjaxFileUpload-Demo/
希望它对您有用。
干杯
Here you'll find a really good example/tutorial how to upload one file http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
Hope it works for you.
Cheers