开发者

Submit a file element with jquery and ajax: No plugins wanted!

开发者 https://www.devze.com 2023-01-21 03:21 出处:网络
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>

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 h开发者_如何学Ctml 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.


If you like, there is jQuery Uploadify plugin to do exactly what you are looking for other than other great features.


I use the jQuery AJAX form plugin on my site to upload files.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消