{"id":14601,"date":"2014-07-04T15:58:34","date_gmt":"2014-07-04T10:28:34","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14601"},"modified":"2017-02-06T10:37:52","modified_gmt":"2017-02-06T05:07:52","slug":"uploading-a-file-using-only-ajax","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/uploading-a-file-using-only-ajax\/","title":{"rendered":"Uploading a file using only Ajax"},"content":{"rendered":"<p>We often need to upload a file without refreshing page or even before user submits the complete form.<\/p>\n<p>We have been using flash uploaders or i-frames to achieve that. However now it is possible to upload a file using only ajax and javascript.<\/p>\n<p>The first thing we need is a HTML form that will allow the user to select the file that they wish to upload. Lets create a form with file type in it<\/p>\n<p>In grails, we prefer to use g:form tag<\/p>\n<p>[java]<br \/>\n&amp;lt;g:form name=&amp;quot;myForm&amp;quot;&amp;gt;<br \/>\n   &amp;lt;div&amp;gt;<br \/>\n      &amp;lt;label for=&amp;quot;name&amp;quot;&amp;gt;<br \/>\n        &amp;lt;strong&amp;gt;Name&amp;lt;\/strong&amp;gt;<br \/>\n      &amp;lt;\/label&amp;gt;<br \/>\n   &amp;lt;input id=&amp;quot;name&amp;quot; name=&amp;quot;name&amp;quot; type=&amp;quot;text&amp;quot;\/&amp;gt;<br \/>\n  &amp;lt;\/div&amp;gt;<br \/>\n  &amp;lt;input type=&amp;quot;file&amp;quot; name=&amp;quot;inputFile&amp;quot;&amp;gt;<br \/>\n&amp;lt;\/g:form&amp;gt;<br \/>\n[\/java]<\/p>\n<p>We can even use a normal form tag:<\/p>\n<p>[java]<br \/>\n&amp;lt;form method=&amp;quot;post&amp;quot; enctype=&amp;quot;multipart\/form-data&amp;quot;&amp;gt;<br \/>\n   &amp;lt;div&amp;gt;<br \/>\n        &amp;lt;label for=&amp;quot;name&amp;quot;&amp;gt;<br \/>\n            &amp;lt;strong&amp;gt;Name&amp;lt;\/strong&amp;gt;<br \/>\n        &amp;lt;\/label&amp;gt;<br \/>\n        &amp;lt;input id=&amp;quot;name&amp;quot; name=&amp;quot;name&amp;quot; type=&amp;quot;text&amp;quot;\/&amp;gt;<br \/>\n   &amp;lt;\/div&amp;gt;<br \/>\n   &amp;lt;input type=&amp;quot;file&amp;quot; id=&amp;quot;inputFile&amp;quot;  name=&amp;quot;inputFile&amp;quot;&amp;gt;<br \/>\n&amp;lt;\/form&amp;gt;<\/p>\n<p>[\/java]<\/p>\n<p>Now lets include jQuery form plugin  which will provide us functionality to upload files using ajax<\/p>\n<p>Now all we need to do is call ajaxSubmit method, which is provided by jQuery form plugin<br \/>\nwith options.<br \/>\n[java]<br \/>\n&amp;lt;script type=&amp;quot;text\/javascript&amp;quot;&amp;gt;<br \/>\n   $(&#8216;form&#8217;).ajaxSubmit({<br \/>\n        async: true,<br \/>\n        url: myCustomUrl,<br \/>\n        success : function(){<br \/>\n           &#8230;.. \/\/ Add your success handler code here<br \/>\n        }<br \/>\n&amp;lt;\/script&amp;gt;<br \/>\n[\/java]<br \/>\nI also created a <a href=\"https:\/\/github.com\/mansi90\/fileUploadAjaxDemo\">demo app<\/a>, showing this behaviour. Feel free to check it out. <\/p>\n<p>Cheers!<\/p>\n<p>Mansi Arora<\/p>\n<p>mansi[at]intelligrape[dot]com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We often need to upload a file without refreshing page or even before user submits the complete form. We have been using flash uploaders or i-frames to achieve that. However now it is possible to upload a file using only ajax and javascript. The first thing we need is a HTML form that will allow [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[96,71,4840,55,1465],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14601"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14601"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14601\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}