title: Downloading PDF from azure fails to load when reopening
body: <p>I am trying to download a pdf file from azure's blob storage in a memory stream. However, when I try to open the file it does not load.</p>
  
  <p>Here is the code I am using.
  <strong>Download Code</strong></p>
  
  <pre><code>    protected void blobDownload_Click(object sender, EventArgs e)
      {
          //Getting the blob storage container values
          DBAccess dbaCon = new DBAccess();
          DataTable dt = dbaCon.GetTrainingRecord(TrainingTrainingRecordID.Value);
          string companyID = dt.Rows[0].Field&lt;string&gt;("fk_company_id");
          string blobStorageName = dt.Rows[0].Field&lt;string&gt;("uploaded_file");
          string filename = dt.Rows[0].Field&lt;string&gt;("display_file_name");
  
          CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
          CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
          CloudBlobContainer container = blobClient.GetContainerReference(System.Text.RegularExpressions.Regex.Replace(companyID.ToLower(), @"\s+", ""));
          CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobStorageName);
          MemoryStream memStream = new MemoryStream();
          blockBlob.DownloadToStream(memStream);
          blockBlob.FetchAttributes();
          HttpResponse response = HttpContext.Current.Response;
          //response.ContentType = blockBlob.Properties.ContentType;
          response.ContentType = "application/pdf";
          response.AddHeader("Content-Disposition", "Attachment; filename=" + filename);
          response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString());
          response.BinaryWrite(memStream.ToArray());
      }
  </code></pre>
  
  <p>Basically the above code is triggered on a button click, a file is downloaded through the browser but when trying to open the file it cannot be loaded. I have checked  in the azure portal and I am able to directly download the file through the azure portal and the file is fine.</p>
  
  <p>I have tried to directly set the content type and that does not seem to work. </p>
  
  <p>Pdf files seem to be the only files that don't work I have tested with .jpg and .png files and it downloads and opens fine.</p>
  
  <p>Thanks</p>
  
  <p><strong>Update</strong></p>
  
  <p>The problem has mostly been fixed as it was an issue with the way I was uploading the files. As I needed to store the files uniquely I had to change the name in blob storage to overcome this I used the <code>.SaveAs()</code> function below.</p>
  
  <p><strong>Upload Code</strong></p>
  
  <pre><code>    private void uploadFile(string blobContainer, string randomFileName)
      {
          // Retrieve storage account from connection string.
          CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
              CloudConfigurationManager.GetSetting("StorageConnectionString"));
  
          // Create the blob client.
          CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
  
          // Retrieve reference to a previously created container.
          CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);
  
          // Create the container if it doesn't already exist.
          container.CreateIfNotExists();
  
          // Retrieve reference to a blob named "randomFileName".
          CloudBlockBlob blockBlob = container.GetBlockBlobReference(randomFileName);
  
          filMyFile.PostedFile.SaveAs(Server.MapPath("~/") + randomFileName);
  
          // Create or overwrite the "myblob" blob with contents from a local file.
          using (filMyFile.PostedFile.InputStream)
          {
              blockBlob.UploadFromStream(filMyFile.PostedFile.InputStream);
          }
      }
  </code></pre>
  
  <p>The file downloads fine in Edge and chrome however in firefox the name gets shortened and the file extension is missing. The following is a screen shot showing the issue.<a href="https://i.stack.imgur.com/CF4aJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CF4aJ.png" alt="Mozilla pdf download"></a></p>