title: File Size Goes to Zero when Returning FileStreamResult from MVC Controller
body: <p>I am attempting to download a file from Azure Storage in the form of an CloudBlockBlob. I want to allow the user to select where to put the downloaded file, so I have written the following code to do this </p>
  
  <pre><code>[AllowAnonymous]
  public async Task&lt;ActionResult&gt; DownloadFile(string displayName)
  {
      ApplicationUser user = null;
      if (ModelState.IsValid)
      {
          user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
  
          // Retrieve storage account and blob client.
          CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
              ConfigurationManager.AppSettings["StorageConnectionString"]);
          CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
          CloudBlobContainer container = blobClient.GetContainerReference(
              VisasysNET.Utilities.Constants.ContainerName);
  
          // If the container does not exist, return error.
          if (container.Exists())
          {
              foreach (IListBlobItem item in container.ListBlobs(null, false))
              {
                  if (item.GetType() == typeof(CloudBlockBlob))
                  {
                      CloudBlockBlob blob = (CloudBlockBlob)item;
                      if (blob.Name.CompareNoCase(displayName))
                      {
                          string contentType = String.Format(
                              "application/{0}", 
                              Path.GetExtension(displayName).TrimStart('.'));
  
                          // No need to dispose, FileStreamResult will do this for us.
                          Stream stream = new MemoryStream();
                          await blob.DownloadRangeToStreamAsync(stream, null, null);
                          return File(stream, contentType, displayName);
                      }
                  }
              }
              return RedirectToAction("Index", "Tools");
          }
      }
      return new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable);
  }
  </code></pre>
  
  <p>This downloads the file from the blob storage fine, but when the controller returns to the view using <code>FileStreamResult</code>, the browser is launching the save file dialog as expected but the file size is 0 bytes. The <code>Stream</code> shows that the correct file size, but when I do </p>
  
  <pre><code> return File(stream, contentType, displayName);
  </code></pre>
  
  <p>the data does not seem to be passed to the save dialog.</p>
  
  <p><strong>How can I get the file to save properly?</strong></p>
  
  <p>Thanks for your time.</p>