{"id":1017,"date":"2015-11-06T05:58:01","date_gmt":"2015-11-06T05:58:01","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1017"},"modified":"2015-11-06T09:43:24","modified_gmt":"2015-11-06T09:43:24","slug":"when-you-download-a-file-response-sent-back-is-file","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/when-you-download-a-file-response-sent-back-is-file\/","title":{"rendered":"When you download a file, Response sent back is File"},"content":{"rendered":"<p>Whenever you submit a form to download a file the response sent back is a file. In this case the setter called after the method which does the download has no effect.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n if(form.getDownloadForm != null &amp;&amp; form.getDownloadForm.equals(&quot;Y&quot;))\r\n {\r\n   downloadForm(form, request);\r\n\r\n   \/\/The Below Setter has No Effect\r\n   form.setDownloadForm(&quot;Y&quot;);\r\n }\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n class RegisterForm()\r\n {\r\n   public String downloadForm;\r\n\r\n    public void setDownloadForm(String p_download) \r\n    {\r\n\t  this.downloadForm= p_download;\r\n    }\r\n    \r\n    public String getDownloadForm() \r\n    {\r\n\t  return downloadForm;\r\n    }\r\n } \r\n<\/pre>\n<p>The Setter had no effect since the request sent to the <strong>downloadForm(form, request);<\/strong> will end up with response of file download and not response which has new values to form set by setter(in our case <em>downloadForm(form, request)<\/em>) and Page reload.<\/p>\n<p>There are 2 simple fix for this. <\/p>\n<p><strong>Fix 1<\/strong><br \/>\nOne simple fix is to set form hidden property in java script to N after the form get submitted. Note though the form is submitted it is not reloaded with new response since the response sent back is a file.<\/p>\n<p><strong>The Page will be reloaded only when response is sent back to the same URL of browser. Not to the browser as file.<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n function downloadWFSetup(pWaterfallId) \r\n {\t\r\n    $('#downloadForm').val('Y');\r\n    document.WaterfallTriggerForm.submit();\r\n\r\n    \/\/This part of code runs even after the form submission since the \r\n    \/\/response sent is file which does not require page reload\r\n    $('#downloadForm').val('N');\t\t\t\r\n }\r\n<\/pre>\n<p>In our case the page is not going to get reloaded so the java script continues its execution even after form submission setting property of  <strong>downloadForm<\/strong> to <strong>N<\/strong>.<\/p>\n<p><strong>Fix 2<\/strong><br \/>\nThe Other way is to send request in Link with <strong>downloadForm=Y<\/strong>. In this case there is no need to to reset the form values as we get the values by <strong>request.getParameter()<\/strong> Method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whenever you submit a form to download a file the response sent back is a file. In this case the setter called after the method which does the download has no effect. if(form.getDownloadForm != null &amp;&amp; form.getDownloadForm.equals(&quot;Y&quot;)) { downloadForm(form, request); \/\/The Below Setter has No Effect form.setDownloadForm(&quot;Y&quot;); } class RegisterForm() { public String downloadForm; public&hellip; <a href=\"https:\/\/codethataint.com\/blog\/when-you-download-a-file-response-sent-back-is-file\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,88,84],"tags":[],"class_list":["post-1017","post","type-post","status-publish","format-standard","hentry","category-file-handling","category-forms","category-software-design"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1017"}],"version-history":[{"count":7,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1017\/revisions"}],"predecessor-version":[{"id":1024,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1017\/revisions\/1024"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}