<%@ Language="VBScript" CodePage=65001 %> <% ' ===================================================================== ' Audiovidas / Chemifactu - Descarga del instalador con conteo REAL ' Plataforma : Classic ASP (VBScript) sobre IIS ' Funcion : incrementa el contador y sirve /Descargas/Chemifactu8.exe ' Conteo : se suma 1 cuando el fichero se SIRVE (no por simple clic) ' Comparte el mismo almacen que contador.asp: contador_descargas.txt ' ===================================================================== Option Explicit Const FOR_READING = 1 Const FOR_WRITING = 2 Const adTypeBinary = 1 Const TAMANO_BLOQUE = 262144 ' 256 KB por bloque Dim sFichero, sNombreDescarga, sContador Dim oFSO, oFile, nDescargas, sContenido Dim oStream, datos sFichero = Server.MapPath("/Descargas/Chemifactu8.exe") sNombreDescarga = "Chemifactu8.exe" sContador = Server.MapPath("contador_descargas.txt") Set oFSO = Server.CreateObject("Scripting.FileSystemObject") ' --- Si el instalador no existe, devolver 404 y salir --- If Not oFSO.FileExists(sFichero) Then Response.Clear Response.Status = "404 Not Found" Response.ContentType = "text/plain" Response.Write "El archivo de descarga no esta disponible." Set oFSO = Nothing Response.End End If ' --- Incrementar el contador con bloqueo de concurrencia --- Application.Lock nDescargas = 0 If oFSO.FileExists(sContador) Then Set oFile = oFSO.OpenTextFile(sContador, FOR_READING, False) If Not oFile.AtEndOfStream Then sContenido = Trim(oFile.ReadAll) If IsNumeric(sContenido) Then nDescargas = CLng(sContenido) End If oFile.Close End If nDescargas = nDescargas + 1 Set oFile = oFSO.OpenTextFile(sContador, FOR_WRITING, True) oFile.Write nDescargas oFile.Close Application.Unlock Set oFile = Nothing Set oFSO = Nothing ' --- Servir el fichero como descarga (streaming por bloques) --- Response.Clear Response.Buffer = True Response.ContentType = "application/octet-stream" Response.AddHeader "Content-Disposition", "attachment; filename=""" & sNombreDescarga & """" Set oStream = Server.CreateObject("ADODB.Stream") oStream.Type = adTypeBinary oStream.Open oStream.LoadFromFile sFichero Response.AddHeader "Content-Length", oStream.Size Do While Not oStream.EOS If Not Response.IsClientConnected Then Exit Do datos = oStream.Read(TAMANO_BLOQUE) Response.BinaryWrite datos Response.Flush Loop oStream.Close Set oStream = Nothing Response.End %>