{"id":14601,"date":"2014-07-04T15:58:34","date_gmt":"2014-07-04T10:28:34","guid":{"rendered":"https:\/\/tothenewco.pro\/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:\/\/tothenewco.pro\/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,"footnotes":""},"categories":[7],"tags":[96,71,4840,55,1465],"class_list":["post-14601","post","type-post","status-publish","format-standard","hentry","category-grails","tag-ajax","tag-file-upload","tag-grails","tag-javascript","tag-jquery-form-js"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Mansi Arora\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Uploading a file using only Ajax | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Uploading a file using only Ajax | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#article\",\"name\":\"Uploading a file using only Ajax | TO THE NEW Blog\",\"headline\":\"Uploading a file using only Ajax\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2014-07-04T15:58:34+05:30\",\"dateModified\":\"2017-02-06T10:37:52+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#webpage\"},\"articleSection\":\"Grails, Ajax, file upload, Grails, javascript, jquery.form.js\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#listItem\",\"name\":\"Uploading a file using only Ajax\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#listItem\",\"position\":3,\"name\":\"Uploading a file using only Ajax\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/\",\"name\":\"Mansi Arora\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a58f57a35d110e644b024b38bf813f047dd8d76cc2354cdd1ac1ccfe5a2a508d?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Mansi Arora\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/\",\"name\":\"Uploading a file using only Ajax | TO THE NEW Blog\",\"description\":\"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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/uploading-a-file-using-only-ajax\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/#author\"},\"datePublished\":\"2014-07-04T15:58:34+05:30\",\"dateModified\":\"2017-02-06T10:37:52+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Uploading a file using only Ajax | TO THE NEW Blog","description":"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","canonical_url":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#article","name":"Uploading a file using only Ajax | TO THE NEW Blog","headline":"Uploading a file using only Ajax","author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/#author"},"publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"},"datePublished":"2014-07-04T15:58:34+05:30","dateModified":"2017-02-06T10:37:52+05:30","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#webpage"},"isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#webpage"},"articleSection":"Grails, Ajax, file upload, Grails, javascript, jquery.form.js"},{"@type":"BreadcrumbList","@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/tothenewco.pro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/tothenewco.pro\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#listItem","name":"Uploading a file using only Ajax"},"previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#listItem","position":3,"name":"Uploading a file using only Ajax","previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/tothenewco.pro\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/tothenewco.pro\/blog\/"},{"@type":"Person","@id":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/#author","url":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/","name":"Mansi Arora","image":{"@type":"ImageObject","@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a58f57a35d110e644b024b38bf813f047dd8d76cc2354cdd1ac1ccfe5a2a508d?s=96&d=mm&r=g","width":96,"height":96,"caption":"Mansi Arora"}},{"@type":"WebPage","@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#webpage","url":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/","name":"Uploading a file using only Ajax | TO THE NEW Blog","description":"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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/#breadcrumblist"},"author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/#author"},"creator":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/#author"},"datePublished":"2014-07-04T15:58:34+05:30","dateModified":"2017-02-06T10:37:52+05:30"},{"@type":"WebSite","@id":"https:\/\/tothenewco.pro\/blog\/#website","url":"https:\/\/tothenewco.pro\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Uploading a file using only Ajax | TO THE NEW Blog","og:description":"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","og:url":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/","og:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Uploading a file using only Ajax | TO THE NEW Blog","twitter:description":"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","twitter:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"14601","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 08:00:25","updated":"2024-02-29 10:44:25","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUploading a file using only Ajax\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/tothenewco.pro\/blog"},{"label":"Grails","link":"https:\/\/tothenewco.pro\/blog\/category\/grails\/"},{"label":"Uploading a file using only Ajax","link":"https:\/\/tothenewco.pro\/blog\/uploading-a-file-using-only-ajax\/"}],"_links":{"self":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/14601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/comments?post=14601"}],"version-history":[{"count":0,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/14601\/revisions"}],"wp:attachment":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/media?parent=14601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/categories?post=14601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/tags?post=14601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}