title: how to call web api to download document to website directory using webclient
body: <p>I am struggling with being able to create a file with its data based on the byte array returned from the WebAPI. The following is my code for making the call to the web api</p>
  
  <pre><code> using (var http = new WebClient())
              {
                  string url = string.Format("{0}api/FileUpload/FileServe?FileID=" + fileID, webApiUrl);
                  http.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
                  http.Headers[HttpRequestHeader.Authorization] = "Bearer " + authCookie.Value;
                  http.DownloadDataCompleted += Http_DownloadDataCompleted;
                  byte[] json = await http.DownloadDataTaskAsync(url);
  
  
  
              }
  </code></pre>
  
  <p>The api code is </p>
  
  <pre><code>[HttpGet]
      [Route("FileServe")]
      [Authorize(Roles = "Admin,SuperAdmin,Contractor")]
      public async Task&lt;HttpResponseMessage&gt; GetFile(int FileID)
      {
          using (var repo = new MBHDocRepository())
          {
              var file = await repo.GetSpecificFile(FileID);
              if (file == null)
              {
                  throw new HttpResponseException(HttpStatusCode.BadRequest);
              }
  
              var stream = File.Open(file.PathLocator, FileMode.Open);
              HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
              response.Content = new StreamContent(stream);
              response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file.FileType);
              return response;
          }
      }
  </code></pre>
  
  <p>I receive a byte array as a response however am unable to create the corresponding file from that byte array. I have no idea how to convert the byte array into the relevant file type (such as jpg, or pdf based on file type in the web api). any help will be appreciated.</p>