Ver Mensaje Individual
  #21 (permalink)  
Antiguo 24/12/2008, 11:51
haga41
 
Fecha de Ingreso: junio-2008
Mensajes: 145
Antigüedad: 16 años, 10 meses
Puntos: 0
Respuesta: asp subir archivos o foto

Hola Adler,

Lo primero de todo,

¡Feliz navidad y un próspero año nuevo para ti y para todos!

Sobre lo último que me comentas dentro del archivo xelupload.asp, hay un Sub Ficheros. De todas formas he creado un class Ficheros después del Class xelupload y mi fichero de xelupload.asp queda así:

Código asp:
Ver original
  1. <% Class xelUpload
  2. ' Maneja los formularios enviados como 'multipart/form-data' (ficheros)
  3.  
  4. Public Ficheros
  5. Private eltosForm
  6.  
  7. '------------------------------------------------------------------------
  8. Private Sub Class_Initialize()
  9.     set Ficheros = Server.CreateObject("Scripting.Dictionary")
  10.     set eltosForm = Server.CreateObject("Scripting.Dictionary")
  11. End Sub
  12. '------------------------------------------------------------------------
  13. Private Sub Class_Terminate()
  14.     if IsObject(Ficheros) then
  15.         Ficheros.RemoveAll
  16.         set Ficheros = nothing
  17.     end if
  18.     if IsObject(eltosForm) then
  19.         eltosForm.RemoveAll
  20.         set eltosForm = nothing
  21.     end if
  22. End Sub
  23. '------------------------------------------------------------------------
  24. 'Permite hacer, por ejemplo: Response.Write(upload.Form("nombre"))
  25. Public Property Get Form(campo)
  26.     if eltosForm.Exists(campo) then
  27.         Form = eltosForm.Item(campo)
  28.     else
  29.         Form = ""
  30.     end if
  31. End Property
  32. '------------------------------------------------------------------------
  33. Public Sub Upload()
  34. 'Inicia el proceso. Debe llamarse ANTES DE HACER CUALQUIER OTRA COSA
  35.  
  36. Dim byteDatos, strControl
  37. Dim iPosInicio, iPosFin, iPos, byteLimite, posLimite
  38. Dim iPosFich, iPosLim
  39.  
  40. byteDatos = Request.BinaryRead(Request.TotalBytes)
  41. iPosInicio = 1
  42. iPosFin = InStrB(iPosInicio, byteDatos, str2byte(chr(13)))
  43. if (iPosFin-iPosInicio) <= 0 then
  44. 'terminamos, no hay nada que leer
  45.     Exit Sub
  46. end if
  47. 'extraemos el limite de principio y fin de los datos (p.e. -----2323g237623)
  48. byteLimite = MidB(byteDatos, iPosInicio, iPosFin-iPosInicio)
  49. posLimite = InStrB(1, byteDatos, byteLimite)
  50.  
  51. 'terminamos cuando la posición del próximo límite sea igual
  52. 'a la del límite final, que lleva "--" detrás.
  53. do until posLimite = InStrB(byteDatos, byteLimite & str2byte("--"))
  54.  
  55.     iPos = InStrB(posLimite, byteDatos, str2byte("Content-Disposition"))
  56.     iPos = InStrB(iPos, byteDatos, str2byte("name=")) 'nombre del control en <FORM>
  57.     iPosInicio = iPos + 6 'me salto 6 caracteres -> name="
  58.     iPosFin = InStrB(iPosInicio, byteDatos, str2byte(chr(34))) 'busco las comillas de cierre
  59.     'y tengo el nombre del control!
  60.     strControl = byte2str(MidB(byteDatos, iPosInicio, iPosFin-iPosInicio))
  61.     'busco ahora los datos en sí del control
  62.     iPosFich =InStrB(posLimite, byteDatos, str2byte("filename="))
  63.     posLimite = InStrB(iPosFin, byteDatos, byteLimite)
  64.    
  65.     '¿fichero o campo del formulario?
  66.     if iPosFich <> 0 and iPosFich < PosLimite then
  67.         'es un fichero, creo un nuevo objeto fichero y lo añado a Ficheros
  68.         Dim oFichero, strNombre, strForm
  69.         set oFichero = new Fichero
  70.        
  71.         iPosInicio = iPosFich + 10 'me salto 10 caracteres -> filename="
  72.         iPosFin = InStrB(iPosInicio, byteDatos, str2byte(chr(34)))
  73.         strNombre = byte2str(MidB(byteDatos, iPosInicio, iPosFin-iPosInicio))
  74.         'quito la ruta inicial
  75.         oFichero.Nombre = Right(strNombre, Len(strNombre)-InStrRev(strNombre, "\")) '"
  76.        
  77.         iPos = InStrB(iPosFin, byteDatos, str2byte("Content-Type:"))
  78.         iPosInicio = iPos + 14 'me salto Content-Type y un espacio!!
  79.         iPosFin = InStrB(iPosInicio, byteDatos, str2byte(chr(13))) 'busco el retorno de carro
  80.         oFichero.TipoContenido = byte2str(MidB(byteDatos, iPosInicio, iPosFin-iPosInicio))
  81.        
  82.         iPosInicio = iPosFin + 4    'me salto los 3 retornos de carro que lleva!!!
  83.         iPosFin = InStrB(iPosInicio, byteDatos, byteLimite)-2 'dos caracteres atrás
  84.         oFichero.Datos = MidB(byteDatos, iPosInicio, iPosFin-iPosInicio)
  85.         if oFichero.Tamano > 0 then 'lo añado a la colección Ficheros!
  86.             Ficheros.Add strControl, oFichero
  87.         end if
  88.     else
  89.         'es un campo del formulario
  90.         iPos = InStrB(iPos, byteDatos, str2byte(chr(13)))
  91.         iPosInicio = iPos + 4
  92.         iPosFin = InStrB(iPosInicio, byteDatos, byteLimite)-2
  93.         'extraigo el valor del control del formulario!
  94.         strForm = byte2str(MidB(byteDatos, iPosInicio, iPosFin-iPosInicio))
  95.         if not eltosForm.Exists(strControl) then
  96.             eltosForm.Add strControl, strForm
  97.         else
  98.             eltosForm.Item(strControl) =  eltosForm.Item(strControl)+","&strForm
  99.         end if
  100.     end if
  101.     'saltamos al siguiente límite
  102.     iPosLimite = InStrB(iPosLimite+LenB(byteLimite), byteDatos, byteLimite)
  103.    
  104.     loop
  105.    
  106. End Sub
  107. '------------------------------------------------------------------------
  108. Private Function str2byte ( str )
  109. Dim i, strbuf
  110. for i = 1 to Len(str)
  111.     strbuf = strbuf & ChrB(AscB(Mid(str, i, 1)))
  112. next
  113. str2byte = strbuf
  114. End Function
  115. '------------------------------------------------------------------------
  116. Private Function byte2str ( bin )
  117. Dim i, bytebuf
  118. for i = 1 to LenB(bin)
  119.     bytebuf = bytebuf & Chr(AscB(MidB(bin, i, 1)))
  120. next
  121. byte2str = bytebuf
  122. End Function
  123. '------------------------------------------------------------------------
  124. End Class
  125.  
  126. '############################ Clase Fichero!!! ##########################
  127.  
  128. Class Fichero
  129. '------------------------------------------------------------------------
  130. Public Nombre
  131. Public TipoContenido
  132. Public Datos
  133.  
  134. '------------------------------------------------------------------------
  135. Public Property Get Tamano()
  136.     Tamano = LenB(Datos)
  137. End Property
  138. '------------------------------------------------------------------------
  139.  
  140.  
  141.  
  142. Public Sub Guardar(ruta)
  143. Dim oFSO, oFich
  144. Dim i
  145.  
  146. if ruta = "" or Nombre = "" then Exit Sub
  147. if Mid(ruta, Len(ruta)) <> "\" then     '" 
  148.     'añado la ultima barra a la ruta
  149.     ruta = ruta & "\"                       '"
  150. end if
  151.  
  152. set oFSO = Server.CreateObject("Scripting.FileSystemObject")
  153. if not oFSO.FolderExists(ruta) then Exit Sub
  154. set oFich = oFSO.CreateTextFile(ruta & Nombre, true)
  155.  
  156. for i = 1 to LenB(Datos)
  157.     oFich.Write Chr(AscB(MidB(Datos, i, 1)))
  158. next    
  159.  
  160. oFich.Close
  161. set oFSO = nothing
  162. End Sub
  163. '------------------------------------------------------------------------
  164. '------------------------------------------------------------------------
  165. Public Sub GuardarComo(nombrefichero, ruta)
  166. Dim oFSO, oFich, i
  167.  
  168. if ruta = "" or nombrefichero = "" then Exit Sub
  169. if Mid(ruta, Len(ruta)) <> "\" then     '" 
  170.     'añado la ultima barra a la ruta
  171.     ruta = ruta & "\"                       '"
  172. end if
  173.  
  174. set oFSO = Server.CreateObject("Scripting.FileSystemObject")
  175. if not oFSO.FolderExists(ruta) then Exit Sub
  176. set oFich = oFSO.CreateTextFile(ruta & nombrefichero, true)
  177.  
  178. for i = 1 to LenB(Datos)
  179.     oFich.Write Chr(AscB(MidB(Datos, i, 1)))
  180. next    
  181.  
  182. oFich.Close
  183. set oFSO = nothing
  184.  
  185.  
  186. End Sub
  187. '------------------------------------------------------------------------
  188. Public Sub GuardarBD (byRef field)
  189. if LenB(Datos) = 0 then Exit Sub
  190.  
  191. field.AppendChunk Datos
  192. End Sub
  193.  
  194.  
  195. '------------------------------------------------------------------------
  196.  
  197.  
  198.  
  199. '---------------------------------
  200. End Class
  201. %>
  202.  
  203. <% Class Ficheros
  204. Public Sub ListFolderContents(path)
  205.           dim objFSO, objFolder, colfiles
  206.          
  207.         Set objFSO = CreateObject("Scripting.FileSystemObject")
  208.         Set objFolder = objFSO.GetFolder(path)
  209.         Set colFiles = objFolder.Files
  210.  
  211.           'Muestro la info de la carpeta (si quiero)
  212.           'Response.Write("<li><b>" & folder.Name & "</b> - " _
  213.           ' & folder.Files.Count & " files, ")
  214.           'Response.Write(Round(folder.Size / 1024) & " KB total." _
  215.           ' & vbCrLf)
  216.           'nothing there.. print
  217.           if colFiles.Count = 0 then
  218.               Response.Write    "<table border=""0"""&_
  219.                               "cellspacing=""1"" cellpadding=""2"""&_
  220.                               "><tr><td>Este archivo no se ha subido.</td></tr></table>"
  221.           else
  222.               '    Response.Write("" & vbCrLf)
  223.               'Display a list of sub folders.
  224.               For Each objFile in colFiles
  225.                        ListFolderContents(item.Path)
  226.               next
  227.               'Display a list of files.
  228.               Response.Write    "<table border=""0"" cellspacing=""1"" cellpadding=""2"">"&_
  229.                               "<tr>"&_
  230.                               "<td>Nombre del archivo</td>"&_
  231.                               "<td>Tamaño</td>"&_
  232.                               "<td>Contenido</td>"&_
  233.                               "<td>Fecha de Modificacion</td>"&_
  234.                               "<td>Borrar</td>"&_
  235.                               "</tr></table>"
  236.  
  237.           end if
  238.       end sub
  239. End class
  240. %>

Después en el otro archivo en el que llamo a la clase Ficheros lo he puesto así:

Código asp:
Ver original
  1. <!--#include file="includes/xelupload.asp"-->
  2.      <%
  3.         id = session("id")
  4.         'Primero subimos el fichero:'
  5.         Dim up, obj
  6.         set up = new xelUpload
  7.         up.Upload()            
  8.        
  9. set obj = up.Ficheros("fichero")      
  10. nombreFoto=obj.nombre
  11.  
  12.  
  13. obj.GuardarComo nombreFoto, Server.MapPath("caratulas/")
  14. obj.ListFolderContents(Server.MapPath("caratulas/") )
  15.        %>

Pero sigue dandome los mismos errores que antes si no subo ninguna foto, me da error de undefined en la línea de: set obj = up.Ficheros("fichero") y si subo foto me da error en el ListFolder con= Error de Microsoft VBScript en tiempo de ejecución (0x800A01B6)
El objeto no acepta esta propiedad o método: 'ListFolderContents'

De todas formas, gracias por la ayuda, ahora mismo lo importante es disfrutar todo lo que se pueda de estos días. Esto se ya se irá arreglando poco a poco, seguro que al final será una tontería pero cuesta averiguar donde está el fallo.

¡Felices fiestas!