Buenas,
después de unos días mas que movidos en Barcelona, donde tuve un par de jornadas intensivas de produccion A FUEGGOOO !!! he sacado en claro lo siguiente:
- Parece que existe una harina que no hace grumos, yo ni siquiera sabia que la harina con grumos es un problema; pero se ve que este invento es mejor que el WWF.
- Para la próxima semana en Sabadell (Barcelona); me tengo que traer un poco mas de ropa
porque parece que 2 dias son igual a 5 y un parapente para pasear; con el viento que hay es la mejor forma de viajar. - Hay muchas formas de bajar un archivo utilizando .Net; antes podíamos utilizar el objeto xmlHttpRequest, que estaba dentro del paquete MSXML2. Ahora con .Net 2.0 y la nueva librería System.Net, las cosas son mucho mas fáciles.
Dentro de esta librería tenemos la clase WebClient, que es realmente fantástica. La misma posee los siguientes métodos y lo interesante de los mismos, es que para bajar un archivo, por ejemplo; ya traen una interfaz para realizar este trabajo en Background.
OpenWrite
Retrieves a Stream used to send data to the resource.
OpenWriteAsync
Retrieves a Stream used to send data to the resource, without blocking the calling thread.
UploadData
Sends a byte array to the resource and returns a Byte array containing any response.
UploadDataAsync
Sends a Byte array to the resource, without blocking the calling thread.
UploadFile
Sends a local file to the resource and returns a Byte array containing any response.
UploadFileAsync
Sends a local file to the resource, without blocking the calling thread.
UploadValues
Sends a NameValueCollection to the resource and returns a Byte array containing any response.
UploadValuesAsync
Sends a NameValueCollection to the resource and returns a Byte array containing any response, without blocking the calling thread.
UploadString
Sends a String to the resource, without blocking the calling thread.
UploadStringAsync
Sends a String to the resource, without blocking the calling thread.
OpenRead
Returns the data from a resource as a Stream.
OpenReadAsync
Returns the data from a resource, without blocking the calling thread.
DownloadData
Downloads data from a resource and returns a Byte array.
DownloadDataAsync
Downloads data from a resource and returns a Byte array, without blocking the calling thread.
DownloadFile
Downloads data from a resource to a local file.
DownloadFileAsync
Downloads data from a resource to a local file, without blocking the calling thread.
DownloadString
Downloads a String from a resource and returns a String.
DownloadStringAsync
Downloads a String from a resource, without blocking the calling thread.
Los nombres son muy claros, pero como siempre 2 lineas de código valen mas que mil comentarios.
1: Public Class Form1
2:
3: Private WithEvents client As New System.Net.WebClient
4:
5: ''' <summary>
6: ''' Handles the Load event of the Form1 control.
7: ''' </summary>
8: ''' <param name="sender">The source of the event.</param>
9: ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
10: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
11:
12: Dim file As String = "www.google.com/images/el_logo_de_google.gif"
13: Dim fileDownload As String = "ellogodegoogle.gif"
14:
15: ' inicia la descarga
16: client.DownloadFileAsync(New System.Uri(file), fileDownload)
17:
18: End Sub
19:
20: ''' <summary>
21: ''' Handles the DownloadFileCompleted event of the client control.
22: ''' </summary>
23: ''' <param name="sender">The source of the event.</param>
24: ''' <param name="e">The <see cref="System.ComponentModel.AsyncCompletedEventArgs" /> instance containing the event data.</param>
25: Private Sub client_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles client.DownloadFileCompleted
26:
27: ' validacion de errores
28: If e.Cancelled Or Not (e.Error Is Nothing) Then
29: MsgBox("ha ocurrido un error durante la descarga") 30: Exit Sub
31: End If
32: MsgBox("Descarga finalizada") 33:
34: End Sub
35:
36: End Class
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
como podemos ver el objeto client es el encargado de realizar todo el trabajo. En la linea 16 se llama a la funcion DownloadFileAsync(), donde se especifica el archivo que deseamos bajar y el nombre del archivo local que queremos crear. Por otra parte en la linea 25, se captura el evento DownloadFileCompleted() y validamos un argumento para ver si el download fue success.
Espero que les sea útil, a mi me ha servido muchisimo.
Saludos
El Bruno