viernes, 10 de febrero de 2017

Descargar PDF con Asp Clásico

Hace unos días estuve buscando en Internet alguna función en ASP clásico que me permitiera descargar archivos PDFs.

Y encontré éstas funciones que permiten descargar un fichero dada la ruta donde se encuentra y el nombre del archivo:

'*******************************FUNCIÓN 1*****************
Function downloadFile(strPathAndFile, strFileName)
    Dim objFso
    Dim objStream
    If strPathAndFile <> "" Then
        Set objFso = Server.CreateObject("Scripting.FileSystemObject")
       'If objFso.FileExists(strPathAndFile) Then
            Response.AddHeader "Content-disposition", "filename=" & strFileName
            Response.ContentType = "application/octet-stream"
            Response.AddHeader "Pragma", "no-cache"
            Response.AddHeader "Expires", "0"
            Set objStream = Server.CreateObject("ADODB.Stream")
            objStream.Type = 1
            objStream.Open
            objStream.LoadFromFile strPathAndFile
            Response.BinaryWrite(objStream.Read())
            objStream.Close
            Set objStream = Nothing
        Else
            Response.Write "La ruta del fichero no existe."
        End If
        Set objFso = Nothing
    End If

End Function

'*******************************FUNCIÓN 2***************** 
Function downloadFile(strPathAndFile, strFileName)
    Response.Buffer = False
    Dim objStream
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Type = 1 'adTypeBinary
    objStream.Open
    objStream.LoadFromFile(strPathAndFile)
    Response.ContentType = "application/x-pdf"
    Response.Addheader "Content-Disposition", "attachment; filename=" & strFileName
    Response.BinaryWrite objStream.Read
    objStream.Close
    Set objStream = Nothing

End Function

Sin embargo, en ambos casos salió el siguiente error "Es posible que la página web http:// ...  esté temporalmente inactiva  o que se haya trasladado definitivamente a otra dirección



Para solucionar este problema tienes que:

1. Incluir el .Mode encima del .Open y con el valor 3 para prevenir errores de bloqueo de acceso si están accediendo al archivo varias personas al mismo tiempo.

objStream.Mode = 3

objStream.Open
objStream.Type = 1


2. Cambiar: Response.ContentType "application/octet-stream"
por: Response.ContentType = "xxx/xxx"

3. Antes de Response.AddHeader Incluir Response.Clear

4. Después de cerrar el FileSystemObject incluir: response.end


Quedando el código como sigue:

Function downloadFile(strFilePath, strFileName)
   
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
   
    If objFSO.FileExists(strFilePath) Then
        Set objFile = objFSO.GetFile(strFilePath)
        intFileSize = objFile.Size
        Set objFile = Nothing

        Response.Clear
        Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName
        Response.ContentType = "xxx/xxx"
        Response.AddHeader "Content-Length", intFileSize

        Set objStream = Server.CreateObject("ADODB.Stream")
        objStream.Mode = 3
        objStream.Open
        objStream.Type = 1
       
        objStream.LoadFromFile strFilePath

        Do While Not objStream.EOS And Response.IsClientConnected
            Response.BinaryWrite objStream.Read(1024)
            Response.Flush()
        Loop

        objStream.Close
        Set objStream = Nothing

    End if
   
    Set objFSO = Nothing
    response.end

End Function

 

1 comentario:

  1. Gracias por copratit esta información, no sabía como descargar PDF y como amante a la lectura no podía descargar ningún libro en PDF. Como ya tengo PDF estoy buscando Te odio como nunca quise a nadie pdf
    para descargar sin errores..

    ResponderEliminar