Descarga de un archivo remoto a un MemoryStream con C #
El siguiente código muestra de una forma sencilla como descargar un archivo y lo colocarlo en la secuencia de memoria. Situación: El Código:
Descargar un archivo PDF desde una ubicación remota y almacenar datos en un MemoryStream.
// Sample Url, Watch for Line Breaks static string pdfUrl = @"http://download.microsoft.com/download/c/a/9/ ca927411-504e-498a-ad2e-490ca4d9cd27/Journal_4_web.pdf"; // Member variable to store the MemoryStream Data private MemoryStream pdfMemoryStream; // Public MemoryStream property containing PDF Data public MemoryStream PdfMemoryStream { get { // Check to see if the MemoryStream has already been created, // if not, then create memory stream if (this.pdfMemoryStream == null) { WebClient client = new WebClient(); try { this.pdfMemoryStream = new MemoryStream(client.DownloadData(pdfUrl)); } finally { client.Dispose(); } } return this.pdfMemoryStream; } }
