title: Download Large File from Azure Blob Storage, process it and send back to the Client
body: <p>I have the following request flow where the customer can request to download a CSV file from the Server. The issue is that the blob file is too large and the customer has to wait a lot longer before the actual download starts (the customer thinks that there is some issue and closes the browser). How can the download be made more efficient using streams?<br />
  Current sequence is as below:</p>
  <p><strong>Request Sequence:</strong></p>
  <ol>
  <li>Client clicks the download button from the browser.</li>
  <li>Backend receives the request.</li>
  <li>Backend Server Downloads the Blob from the Azure Storage Account.</li>
  <li>There is some custom processing that needs to be done.</li>
  <li>Once the processing is completed, start sending the response back to the client.</li>
  </ol>
  <p>Now the issue is that while using the <a href="https://docs.microsoft.com/en-us/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadto?view=azure-dotnet#Azure_Storage_Blobs_Specialized_BlobBaseClient_DownloadTo_System_IO_Stream_Azure_Storage_Blobs_Models_BlobRequestConditions_Azure_Storage_StorageTransferOptions_System_Threading_CancellationToken_" rel="nofollow noreferrer">DownloadTo(Stream)</a> function of BlobBaseClient, the file is entirely downloaded to memory before I could do anything.<br />
  How can I download the blob file in chunks, do the processing and start sending it to the customer?</p>
  <p><strong>Part of Download Controller:</strong></p>
  <pre><code>        var contentDisposition = new ContentDispositionHeaderValue(&quot;attachment&quot;)
          {
              FileName = &quot;customer-file.csv&quot;,
              CreationDate = DateTimeOffset.UtcNow
          };
          Response.Headers.Add(&quot;Content-Disposition&quot;, contentDisposition.ToString());
          var result = blobService.DownloadAndProcessContent();
          foreach (var line in result)
          {
              yield return line ;
          }
          Response.BodyWriter.FlushAsync();
  </code></pre>
  <p><strong>Part of DownloadAndProcessContent Function:</strong></p>
  <pre><code>        var stream = new MemoryStream();
          var blob = container.GetAppendBlobClient(blobName);
          blob.DownloadTo(stream);
          // Processing is done on the Blob Data
          var streamReader = new StreamReader(stream);
          while (!streamReader.EndOfStream)
          {
              string currentLine= streamReader.ReadLine();
              // process the line.
              string processDataLine = ProcessData(currentLine);
              yield return processDataLine;
          }
  </code></pre>