Foros del Web » Programando para Internet » ASP Clásico »

upload de archivos con asp

Estas en el tema de upload de archivos con asp en el foro de ASP Clásico en Foros del Web. Buenas tardes. he buscando como subir archivos con asp y me he encontrado con varios ejemplos unos que funcionan y otros que no un compañero ...
  #1 (permalink)  
Antiguo 19/09/2012, 16:01
 
Fecha de Ingreso: febrero-2012
Ubicación: bogota
Mensajes: 104
Antigüedad: 12 años, 2 meses
Puntos: 0
upload de archivos con asp

Buenas tardes. he buscando como subir archivos con asp y me he encontrado con varios ejemplos unos que funcionan y otros que no un compañero me recomendo un codigo que se llama Freeaspupload los he probado y funciona muy bien, pero he mirado el codigo para trata de entenderlo y no he logrado encontrar en donde tomo el control del archivo para que solo suban archivos con extension .txt o .doc o .pdf si me pudiesen ayudar se los agradeceria.

si necesitan el codigo me avisa para ponerlo.

gracias de atemano.
  #2 (permalink)  
Antiguo 20/09/2012, 14:31
Avatar de Alexis_Mejias  
Fecha de Ingreso: enero-2005
Ubicación: Santiago
Mensajes: 77
Antigüedad: 19 años, 3 meses
Puntos: 17
Respuesta: upload de archivos con asp

En Asp existen muchos componentes que existen por la web para trabajar archivos:
Yo uso uno gratuito que me ha funcionado excelentemente.

Te ajunto el codigo por secciones:

Insert.asp
Código HTML:
<!--#include file="Loader.asp"-->
<%
   Response.Buffer = True  
   

   ' load object
   Dim load
   Set load = new Loader
      
   ' calling initialize method
   load.initialize
   
   ' Value of text input field "name"
   Dim nameInput
   Dim ddlcliente
   
      nameInput = load.getValue("idProNew")
	  ddlcliente  = load.getValue("ddlcliente")
	  TipoArchivo  = load.getValue("tbTipoArchivo")
	  Origen = load.getValue("Origen")
      
   ' File binary data
   Dim fileData
      fileData = load.getFileData("file")
   ' File name
   Dim fileName
      fileName = LCase(load.getFileName("file"))
   ' File path
   Dim filePath
      filePath = load.getFilePath("file")
   ' File path complete
   Dim filePathComplete
      filePathComplete = load.getFilePathComplete("file")
   ' File size
   Dim fileSize
      fileSize = load.getFileSize("file")
      if cdbl(fileSize) > 5197153 then
		  Response.Redirect("SubeContrato.asp?msgError=1&idPro="& nameInput)
		  Response.End
	  END IF
   ' File size translated
   Dim fileSizeTranslated
      fileSizeTranslated = load.getFileSizeTranslated("file")
   ' Content Type
   Dim contentType
      contentType = load.getContentType("file")      
      'and contentType <> "application/msword"
      if contentType <> "application/pdf" AND contentType <> "application/msword" then
		  Response.Redirect("SubeContrato.asp?msgError=2&idPro="& nameInput)
		  Response.End
	  END IF
   ' No. of Form elements
   Dim countElements
      countElements = load.Count   
   ' Path where file will be uploaded
   Dim pathToFile
   if lcase(contentType) = "application/pdf" then
   
	  set fso = server.CreateObject("Scripting.filesystemobject")
	  for nm = 0 to Ubound(vLetras)	  
		str = Server.MapPath("Doctos\"&TipoArchivo&"_"& nameInput & vLetras(nm) & ".pdf")	
		if not fso.FileExists (str) then
			exit for				
		End if
	  next	  
	  set fso = nothing
	  
   
	  pathToFile = str
	'  elseif contentType = "application/msword" then
	'  pathToFile = Server.mapPath("Doctos/") & "\Contrato_" & nameInput &  ".doc" 
   end if
   if lcase(contentType) = "application/msword" then
   
	  set fso = server.CreateObject("Scripting.filesystemobject")
	  for nm = 0 to Ubound(vLetras)	  
		str = Server.MapPath("Doctos\"&TipoArchivo&"_"& nameInput & vLetras(nm) & ".doc")	
		if not fso.FileExists (str) then
			exit for				
		End if
	  next	  
	  set fso = nothing
	   
	  pathToFile = str
   end if
   ' Uploading file data
   Dim fileUploaded
      fileUploaded = load.saveToFile ("file", pathToFile)
      
   ' destroying load object
   Set load = Nothing
Loader.asp
Código HTML:
<%
  ' -- Loader.asp --
  ' -- version 1.5
  ' -- last updated 6/13/2002
  
  Class Loader
    Private dict
    
    Private Sub Class_Initialize
      Set dict = Server.CreateObject("Scripting.Dictionary")
    End Sub
    
    Private Sub Class_Terminate
      If IsObject(intDict) Then
        intDict.RemoveAll
        Set intDict = Nothing
      End If
      If IsObject(dict) Then
        dict.RemoveAll
        Set dict = Nothing
      End If
    End Sub

    Public Property Get Count
      Count = dict.Count
    End Property

    Public Sub Initialize
      If Request.TotalBytes > 0 Then
        Dim binData
          binData = Request.BinaryRead(Request.TotalBytes)
          getData binData
      End If
    End Sub
    
    Public Function getFileData(name)
      If dict.Exists(name) Then
        getFileData = dict(name).Item("Value")
        Else
        getFileData = ""
      End If
    End Function

    Public Function getValue(name)
      Dim gv
      If dict.Exists(name) Then
        gv = CStr(dict(name).Item("Value"))
        
        gv = Left(gv,Len(gv)-2)
        getValue = gv
      Else
        getValue = ""
      End If
    End Function
    
    Public Function saveToFile(name, path)
      If dict.Exists(name) Then
        Dim temp
          temp = dict(name).Item("Value")
        Dim fso
          Set fso = Server.CreateObject("Scripting.FileSystemObject")
        Dim file
            Set file = fso.CreateTextFile(path)
            For tPoint = 1 to LenB(temp)
                file.Write Chr(AscB(MidB(temp,tPoint,1)))
            Next
            file.Close
          saveToFile = True
      Else
          saveToFile = False
      End If
    End Function
    
    Public Function getFileName(name)
      If dict.Exists(name) Then
        Dim temp, tempPos
          temp = dict(name).Item("FileName")
          tempPos = 1 + InStrRev(temp, "\")
          getFileName = Mid(temp, tempPos)
      Else
        getFileName = ""
      End If
    End Function
    
    Public Function getFilePath(name)
      If dict.Exists(name) Then
        Dim temp, tempPos
          temp = dict(name).Item("FileName")
          tempPos = InStrRev(temp, "\")
          getFilePath = Mid(temp, 1, tempPos)
      Else
        getFilePath = ""
      End If
    End Function
    
    Public Function getFilePathComplete(name)
      If dict.Exists(name) Then
        getFilePathComplete = dict(name).Item("FileName")
      Else
        getFilePathComplete = ""
      End If
    End Function

    Public Function getFileSize(name)
      If dict.Exists(name) Then
        getFileSize = LenB(dict(name).Item("Value"))
      Else
        getFileSize = 0
      End If
    End Function

    Public Function getFileSizeTranslated(name)
      If dict.Exists(name) Then
        temp = 1 + LenB(dict(name).Item("Value"))
          If Len(temp) <= 3 Then
            getFileSizeTranslated = temp & " bytes"
          ElseIf Len(temp) > 6 Then
            temp = FormatNumber(((temp / 1024) / 1024), 2)
            getFileSizeTranslated = temp & " megabytes"  
          Else
            temp = FormatNumber((temp / 1024), 2)
            getFileSizeTranslated = temp & " kilobytes"
          End If
      Else
        getFileSizeTranslated = ""
      End If
    End Function
    
    Public Function getContentType(name)
      If dict.Exists(name) Then
        getContentType = dict(name).Item("ContentType")
      Else
        getContentType = ""
      End If
    End Function

  Private Sub getData(rawData)
    Dim separator 
      separator = MidB(rawData, 1, InstrB(1, rawData, ChrB(13)) - 1)

    Dim lenSeparator
      lenSeparator = LenB(separator)

    Dim currentPos
      currentPos = 1
    Dim inStrByte
      inStrByte = 1
    Dim value, mValue
    Dim tempValue
      tempValue = ""

    While inStrByte > 0
      inStrByte = InStrB(currentPos, rawData, separator)
      mValue = inStrByte - currentPos

      If mValue > 1 Then
        value = MidB(rawData, currentPos, mValue)

        Dim begPos, endPos, midValue, nValue
        Dim intDict
          Set intDict = Server.CreateObject("Scripting.Dictionary")
    
          begPos = 1 + InStrB(1, value, ChrB(34))
          endPos = InStrB(begPos + 1, value, ChrB(34))
          nValue = endPos

        Dim nameN
          nameN = MidB(value, begPos, endPos - begPos)

        Dim nameValue, isValid
          isValid = True
          
          If InStrB(1, value, stringToByte("Content-Type")) > 1 Then

            begPos = 1 + InStrB(endPos + 1, value, ChrB(34))
            endPos = InStrB(begPos + 1, value, ChrB(34))
  
            If endPos = 0 Then
              endPos = begPos + 1
              isValid = False
            End If
            
            midValue = MidB(value, begPos, endPos - begPos)
              intDict.Add "FileName", trim(byteToString(midValue))
                
            begPos = 14 + InStrB(endPos + 1, value, stringToByte("Content-Type:"))
            endPos = InStrB(begPos, value, ChrB(13))
            
            midValue = MidB(value, begPos, endPos - begPos)
              intDict.Add "ContentType", trim(byteToString(midValue))
            
            begPos = endPos + 4
            endPos = LenB(value)
            
            nameValue = MidB(value, begPos, endPos - begPos)
          Else
            nameValue = trim(byteToString(MidB(value, nValue + 5)))
          End If

          If isValid = true Then
            intDict.Add "Value", nameValue
            intDict.Add "Name", nameN

            dict.Add byteToString(nameN), intDict
          End If
      End If

      currentPos = lenSeparator + inStrByte
    Wend
  End Sub
  
  End Class

  Private Function stringToByte(toConv)
    Dim tempChar
     For i = 1 to Len(toConv)
       tempChar = Mid(toConv, i, 1)
      stringToByte = stringToByte & chrB(AscB(tempChar))
     Next
  End Function

  Private Function byteToString(toConv)
    For i = 1 to LenB(toConv)
      byteToString = byteToString & chr(AscB(MidB(toConv,i,1))) 
    Next
  End Function
%>
__________________
Alexis Mejias C.
Freelancer Chile - Si te sirvió puntúa positivo
Blog Personal
  #3 (permalink)  
Antiguo 24/09/2012, 10:08
 
Fecha de Ingreso: febrero-2012
Ubicación: bogota
Mensajes: 104
Antigüedad: 12 años, 2 meses
Puntos: 0
Respuesta: upload de archivos con asp

una ultima pregunta me podrias enviar el formato de la pag de html con la que haces la seleccion del archivo para subir.
  #4 (permalink)  
Antiguo 24/09/2012, 12:13
Avatar de Alexis_Mejias  
Fecha de Ingreso: enero-2005
Ubicación: Santiago
Mensajes: 77
Antigüedad: 19 años, 3 meses
Puntos: 17
Respuesta: upload de archivos con asp

Ahi va!

Página HTML
Código HTML:
<form method="POST" enctype="multipart/form-data" action="Insert.asp" ID="Form1" name="frm">
Archivo file: <input type="file" name="file" size="85" id="file">
<br />
		<div align="center">
		    <input type="submit" name="cmdSubeCaptaciones" value="Sube Archivo" class="boton">
		</div>
</form> 
Saludos!
__________________
Alexis Mejias C.
Freelancer Chile - Si te sirvió puntúa positivo
Blog Personal

Etiquetas: asp, funcion, upload
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:42.