Saltar al contenido

Ejemplo de Secuencia de Comandos: Descargar Archivo

Este ejemplo demuestra una acción que descarga un archivo y guarda los datos binarios en una celda.

Esquema de Tabla

Columna Tipo de datos Clave principal Generar automáticamente Anulable
Url NVARCHAR(500) No No No
Size INTEGER No No No
Content BINARY No No No

Secuencia de Comandos

using System.IO;
using System.Net.Http;

var url = Row["Url"].GetValueAsString();

var client = new HttpClient();

byte[] bytes;

using (HttpResponseMessage response = await client.GetAsync(url))
{
    if (!response.IsSuccessStatusCode)
    {
        const string message = "Failed to download file.";
        throw new InvalidOperationException(message);
    }

    bytes = await response.Content.ReadAsByteArrayAsync();
}

Row["Size"].Value = bytes.Length;
Row["Content"].Value = bytes;