title: Phonegap download file from API not Server
body: <p>I am developing a Phonegap/Cordova app and I was able to download a file and put it in the root folder of my phone using the FileTransfer plugin. The file was a simple image hosted on a server. </p>
  
  <p>However, I am trying to download a file that is generated by my API (file is not hosted on the server). Once you hit the api, it returns a binary stream of file. This code works perfectly fine when done using AJAX in a browser but it does not work on my Android phone (testing). When I do this, I get error code 1.</p>
  
  <p>If my URI is of a file hosted on the actual server, my fileTransfer.download works perfectly well.</p>
  
  <p>My API that returns the file is in this format:</p>
  
  <p><a href="http://api.mydomainname.com/file/23" rel="nofollow">http://api.mydomainname.com/file/23</a></p>
  
  <p>where 23 is file id.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>document.addEventListener("deviceready", onDeviceReady, true);
  
  var store;
  var assetURL = encodeURI("http://api.mydomainname.com/file/23");
  var fileName = "test.xlsx";
  
  function onDeviceReady() {
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotSys, fail);
  }
  
  function gotSys(fileSystem) {
      alert("Got FS");
      store = fileSystem.root;
      alert('Checking file: ' + store.toURL() + fileName);
      window.resolveLocalFileSystemURL(store.toURL() + fileName, appStart, downloadAsset);
  }
  
  function downloadAsset() {
      var fileTransfer = new FileTransfer();
      alert("About to start transfer");
      fileTransfer.download(assetURL, store.toURL() + fileName,
          function (entry) {
              alert("Success!");
          },
          function (err) {
              alert("Download error : " + err.code);
          });
  }
  
  function appStart() {
      alert('file exists');
      alert(store.toURL() + fileName);
  }
  
  function fail(error) {
      alert("fail error: " + error);
  }
  </code></pre>
  
  <p>My C# return that I tried (one of the tries) is:</p>
  
  <pre><code>response = new HttpResponseMessage(HttpStatusCode.OK); 
  response.Content = new StreamContent(new MemoryStream(docData)); 
  
  response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
  
  response.Content.Headers.Add("Content-Type", "application/octet-stream");
  response.Content.Headers.ContentDisposition.FileName = doc.DocumentName; 
  return response;
  </code></pre>
  
  <p>I tried to return binary, json (with file binary content), etc. Any suggestions? Is it even possible to download a file that's not hosted on the server?</p>
  
  <p>Thanks</p>