{"id":75,"date":"2014-02-07T16:24:47","date_gmt":"2014-02-07T16:24:47","guid":{"rendered":"http:\/\/www.tvarwebu.cz\/blog\/?p=75"},"modified":"2014-02-07T16:34:49","modified_gmt":"2014-02-07T16:34:49","slug":"updated-script-for-downloading-file-via-ajax","status":"publish","type":"post","link":"https:\/\/www.tvarwebu.cz\/blog\/2014\/02\/updated-script-for-downloading-file-via-ajax\/","title":{"rendered":"Updated script for downloading file from javascript"},"content":{"rendered":"<p>I had to use the ajax to download a file internally on our <a title=\"forms mobile app for managing custom qustionaire\" href=\"https:\/\/www.nestforms.com\/\">NestForms<\/a> project. I was searching for a solution and found one on <a href=\"http:\/\/www.filamentgroup.com\/lab\/jquery_plugin_for_requesting_ajax_like_file_downloads\/\">Filamentgroup<\/a> website.<\/p>\n<p>Unforutunatelly, there were some issues that it did not correctly encoded the request. So I had to update the script in order to make it working. See the code below. I hope that it will help you solve your problems.<\/p>\n<pre lang=\"javascript\">jQuery.download = function(url, data, method){\r\n  \/\/url and data options required\r\n  if( url &amp;&amp; data ){\r\n    \/\/data can be string of parameters or array\/object\r\n    data = typeof data == 'string' ? data : jQuery.param(data);\r\n    \/\/split params into form inputs\r\n    var inputs = new Array();\r\n    jQuery.each(data.split('&amp;'), function(){\r\n      var pair = this.split('=');\r\n      inputs[inputs.length] = $('&lt;input name=\"'+ decodeURIComponent(pair[0]) +'\" type=\"hidden\" \/&gt;')\r\n        .val(decodeURIComponent( pair[1].replace(\/\\+\/g, \" \")));\r\n    });\r\n    \/\/send request\r\n    jQuery('&lt;form action=\"'+ url +'\" method=\"'+ (method||'post') +'\"&gt;'+'&lt;\/form&gt;').append(inputs)\r\n    .appendTo('body').submit().remove();\r\n  };\r\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I had to use the ajax to download a file internally on our NestForms project. I was searching for a solution and found one on Filamentgroup website. Unforutunatelly, there were some issues that it did not correctly encoded the request. So I had to update the script in order to make it working. See the &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/www.tvarwebu.cz\/blog\/2014\/02\/updated-script-for-downloading-file-via-ajax\/\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-uncategorized","item-wrap"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":14,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions\/89"}],"wp:attachment":[{"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}