title: Dealing with attachment file in jquery.ajax
body: <p>In my web page I have a button for downloading a file. If there's no file to download, then I'd like to show a message while staying on the same page. The following javascript code works when there's a file to download (id is file id):</p>
  
  <pre><code>function downloadFile(id) {
      window.location = '/myapp.com/download_file/' + id;
  }
  </code></pre>
  
  <p>My server writes the file content into the response body and sets 'Content-Disposition' header to 'attachment'.</p>
  
  <p>Now, when there's no file I'd like to return some text saying that there was no file to download. So I'd like to do something like below:</p>
  
  <pre><code>function downloadFile(id) {
      $.ajax({
          url: '/myapp.com/download_file/' + id,
          success: function () {
              //if (attachemnt) {
              //    download file: just pass response to the browser?
              //} else {
              //    show error text
              //}
          },
          error: function () {
            //show error message
          }
      });
  }
  </code></pre>
  
  <p>So, my question is:</p>
  
  <ol>
  <li>How can I implement the above functionality?</li>
  <li>Specifically, if there's an attachment file in the response, is it possible just to pass the response to the browser, so that the browser can handle file download?</li>
  </ol>
  
  <p><strong>UPDATE</strong>:
  I can handle 'no file' case now:</p>
  
  <pre><code>$.ajax({
      url: '{{ path('download_prev_other_data', {'other_data_id':form.vars.value.getOtherDataId() }) }}',
      success: function (data, textStatus, jqXHR) {
          var header = jqXHR.getResponseHeader('Content-Disposition');
          if (header &amp;&amp; header.indexOf('attachment') === 0) {
              //pass the original response to the browser
          } else {
              alert(data); //data is just text in my case explaining there was no file
          }
      },
      error: function (jqXHR, textStatus, errorThrown) {
          alert(errorThrown);
      }
  });
  </code></pre>
  
  <p>So, my 2nd question, "how to pass the original response to the browser" is not answered yet.</p>