Autoit en español
Aplicaciones con Autoit - Juan Antonio Villalpando
-- Tutorial de iniciación a Autoit --
Volver al índice del tutorial
____________________________
73.- Bajar archivo de internet con barra de progreso.
- En esta dirección se encuentra el interesante programa multimedia para oir música y ver vídeo VLC:
- http://mirror.de.leaseweb.net/videolan/vlc/2.2.4/win32/vlc-2.2.4-win32.exe ocupa 29 MB.
- Vamos a crear una aplicación con Autoit que baje ese archivo de 29 MB y lo guarde en nuestra carpeta de Windows C:\Temp
- Además mientras se va bajando ese archivo a nuestro ordenador, saldrá una barra de progreso de bajada.
- Seguiremos este mensaje:
https://www.autoitscript.com/forum/topic/186324-calculate-and-reduce-file-size-during-download/#comment-1338099
_____________________________________________________
1.- Aplicación en AutoIt para bajar un archivo y guardarlo en nuestro ordenador.
- Ejecútalo y luego consulta la carpeta C:\Temp, ahí estará el archivo vlc-2.2.4-win32.exe
- No tiene formulario, solo sale el ventanita del progreso de bajada.
- El cerebro de la bajada es InetGet.
https://www.autoitscript.com/autoit3/docs/functions/InetGet.htm
https://www.autoitscript.com/autoit3/docs/functions/InetGetSize.htm
https://www.autoitscript.com/autoit3/docs/functions/InetGetInfo.htm
https://www.autoitscript.com/autoit3/docs/functions/ProgressSet.htm
Código para el AutoIt
|
ProgressOn("", "Bajando VLC...", "0%", 100, 100, 16)
$iPlaces = 2
$url = 'http://mirror.de.leaseweb.net/videolan/vlc/2.2.4/win32/vlc-2.2.4-win32.exe'
$fldr = 'C:\Temp\VLC.exe'
$hInet = InetGet($url, $fldr, 1, 1)
$URLSize = InetGetSize($url)
While Not InetGetInfo($hInet, 2)
Sleep(100)
$Size = InetGetInfo($hInet, 0)
$Percentage = Int($Size / $URLSize * 100)
$iSize = $URLSize - $Size
ProgressSet($Percentage, _GetDisplaySize($iSize, $iPlaces = 2) & " " & $Percentage & " %")
WEnd
ProgressOff()
Func _GetDisplaySize($iTotalDownloaded, Const $iPlaces)
Local Static $aSize[4] = ["Bytes", "KB", "MB", "GB"]
For $i = 0 to 3
$iTotalDownloaded /= 1024
If (Int($iTotalDownloaded) = 0) Then Return Round($iTotalDownloaded * 1024, $iPlaces) & " " & $aSize[$i]
Next
EndFunc
|
_____________________________________________________
2.- Baja archivo y tu eliges en qué carpeta lo guardas.
- Se basa en el ejemplo anterior, se baja archivo. Sale una ventana para la elección de la carpeta donde lo quieres guardar.
- Utilizaremos la función SaveAsDialog: https://www.autoitscript.com/autoit3/docs/functions/FileSaveDialog.htm para indicar dónde lo queremos guardar.
Código para el AutoIt con Diálogo para guardar.
|
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Bajar archivo - Juan A. Villalpando", 613, 431, 192, 132)
$Button1 = GUICtrlCreateButton("Pulsa para bajar ese archivo", 96, 96, 417, 49)
$Label1 = GUICtrlCreateLabel("Escribe la dirección completa del archivo que quieres bajar.", 104, 16, 361, 20)
$Input1 = GUICtrlCreateInput("http://mirror.de.leaseweb.net/videolan/vlc/2.2.4/win32/vlc-2.2.4-win32.exe", 56, 48, 489, 24)
; Juan A. Villalpando - KIO4.COM
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
HotKeySet("{ESC}", "Parar_progreso") ; Pulsar Escape para parar la ventanida de Progreso.
Local $disco = "", $directorio = "", $nombre_archivo = "", $extension = ""
While 1
$msg = GuiGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $Button1
$direccion = GUICtrlRead($Input1)
$Partes = _PathSplit($direccion, $disco, $directorio, $nombre_archivo, $extension) ; Obtener el nombre del archivo
$iPlaces = 2
ProgressOn("", "Bajando archivo...", "0%", 100, 100, 16) ; Ventanita de bajada.
$donde_guardo = FileSaveDialog ("Carpeta donde guardar.", @DocumentsCommonDir,"" ,"", $nombre_archivo & $extension)
$hInet = InetGet($direccion, $donde_guardo, 1, 1)
$URLSize = InetGetSize($direccion) ; Tamaño del archivo
While Not InetGetInfo($hInet, 2) ; Mientras no termine de bajar hacer esto...
Sleep(100)
$Size = InetGetInfo($hInet, 0)
$Percentage = Int($Size / $URLSize * 100)
$iSize = $URLSize - $Size
ProgressSet($Percentage, _GetDisplaySize($iSize, $iPlaces = 2) & " " & $Percentage & " %")
WEnd
ProgressOff()
EndSelect
Wend
Exit
Func _GetDisplaySize($iTotalDownloaded, Const $iPlaces)
Local Static $aSize[4] = ["Bytes", "KB", "MB", "GB"]
For $i = 0 to 3
$iTotalDownloaded /= 1024
If (Int($iTotalDownloaded) = 0) Then Return Round($iTotalDownloaded * 1024, $iPlaces) & " " & $aSize[$i]
Next
EndFunc
Func Parar_progreso()
ProgressOff()
EndFunc
|
_____________________________________________________
- Comentarios.
- No confundir FileOpenDialog que se utilizar para elegir un archivo para luego leerlo, subirlo a Internet, borrarlo,... lo vimos en 72_elegir_archivo_subir.htm
- FileSaveDialog que es para elegir dónde se guardará un archivo.
- Al pulsar la tecla ESC, paramos la ventanita de progreso, pero la bajada continúa. Probando con
InetClose ($hInet), sigue sin parar. No se puede cerrar la formulario hasta que no termine de bajar. De todos modos, baja en segundo plano, es decir mientras está bajando podemos realizar otras acciones en el ordenador.
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
- Baja un archivo de imagen y lo presenta en un Pics.
- Un Pics puede contener imágenes de tipo BMP, JPG y GIF (no animados).
- Hay algunos sitios web que no dejan bajar sus archivos de imágenes.
Código para el AutoIt con Diálogo para guardar.
|
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Bajar archivo - Juan A. Villalpando", 613, 431, 192, 132)
$Button1 = GUICtrlCreateButton("Pulsa para bajar ese archivo", 96, 96, 417, 49)
$Label1 = GUICtrlCreateLabel("Escribe la dirección completa del archivo que quieres bajar.", 104, 16, 361, 20)
$Input1 = GUICtrlCreateInput("http://photos.wikimapia.org/p/00/00/31/40/26_big.jpg", 56, 40, 489, 24)
$Pic1 = GUICtrlCreatePic("", 136, 184, 369, 233)
; Juan A. Villalpando - KIO4.COM
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
HotKeySet("{ESC}", "Parar_progreso") ; Pulsar Escape para parar la ventanida de Progreso.
Local $disco = "", $directorio = "", $nombre_archivo = "", $extension = ""
While 1
$msg = GuiGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $Button1
$direccion = GUICtrlRead($Input1)
$Partes = _PathSplit($direccion, $disco, $directorio, $nombre_archivo, $extension) ; Obtener el nombre del archivo
$iPlaces = 2
ProgressOn("", "Bajando archivo...", "0%", 100, 100, 16) ; Ventanita de bajada.
$donde_guardo = FileSaveDialog ("Carpeta donde guardar.", @DocumentsCommonDir,"" ,"", $nombre_archivo & $extension)
$hInet = InetGet($direccion, $donde_guardo, 1, 1)
$URLSize = InetGetSize($direccion) ; Tamaño del archivo
While Not InetGetInfo($hInet, 2) ; Mientras no termine de bajar hacer esto... Progreso.
Sleep(100)
$Size = InetGetInfo($hInet, 0)
$Percentage = Int($Size / $URLSize * 100)
$iSize = $URLSize - $Size
ProgressSet($Percentage, _GetDisplaySize($iSize, $iPlaces = 2) & " " & $Percentage & " %")
WEnd
ProgressOff()
MsgBox(0,"Dirección de almacenamiento.", $donde_guardo)
GUICtrlSetImage($Pic1, $donde_guardo)
EndSelect
Wend
Exit
Func _GetDisplaySize($iTotalDownloaded, Const $iPlaces)
Local Static $aSize[4] = ["Bytes", "KB", "MB", "GB"]
For $i = 0 to 3
$iTotalDownloaded /= 1024
If (Int($iTotalDownloaded) = 0) Then Return Round($iTotalDownloaded * 1024, $iPlaces) & " " & $aSize[$i]
Next
EndFunc
Func Parar_progreso()
ProgressOff()
EndFunc
|
_____________________________________________________
- Propuesta.
_________________
|