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

La imagen sube al servidor pero la noticia no a una base de datos.

Estas en el tema de La imagen sube al servidor pero la noticia no a una base de datos. en el foro de ASP Clásico en Foros del Web. Estimados amigos: Sigo con un problema al subir al mismo tiempo una imagen al servidor y una noticia a la base de datos. El codigo ...
  #1 (permalink)  
Antiguo 16/09/2004, 12:31
Avatar de armandoweb  
Fecha de Ingreso: enero-2003
Ubicación: San Cristobal, Santa Fe, Argentina
Mensajes: 544
Antigüedad: 21 años, 4 meses
Puntos: 7
La imagen sube al servidor pero la noticia no a una base de datos.

Estimados amigos:

Sigo con un problema al subir al mismo tiempo una imagen al servidor y una noticia a la base de datos.
El codigo siguiente sube la imagen al servidor, pero la noticia no la ingresa en la base de datos. Esto es curioso porque utilizaba el mismo codigo para subir la noticia sola y no tenia problema alguno. Ahora quiero incorporarle la imagen a la noticia y no logro hacerlo. Aqui esta el codigo:

<%
Set Upload = Server.CreateObject("Persits.Upload.1")

Upload.OverwriteFiles = False
Upload.SetMaxSize 1048576
Upload.SaveVirtual "../noticias/images/"

Set File = Upload.Files("file")
If Not File Is Nothing Then
con = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("../base/cybernetsc.mdb")
Set rs = Server.CreateObject("adodb.recordset")
SQL = "SELECT * FROM noticias"
rs.Open SQL, con, 2 ,3

rs.AddNew
rs("imagen") = File.FileName
rs("fecha") = Upload.Form("fecha")
rs("titulo") = Upload.Form("titulo")
rs("copete") = Upload.Form("copete")
rs("noticia") = Upload.Form("noticia")
rs.Update

rs.Close
set rs=nothing
set con=nothing
Response.Redirect "confirma.htm"
Else
Response.Redirect("formulario.htm") ' formulario de donde recoges los datos --> Te llevara aca si es k no haz seleccionado ninguna imagen, en el form anterior
End If
%>


El codigo me envía a formulario.htm, sin insertar la noticia pero la imagen si esta en el servidor en l carpeta indicada.
Si alguien puede ayudarme por favor se lo agradecere eternamente. Si alguien tiene algun ejemplo distinto para realizar lo mismo por favor me lo envian como ejemplo.
Saludos a todos y gracias.

ARIEL CRIPPA
  #2 (permalink)  
Antiguo 16/09/2004, 12:46
Avatar de Saruman  
Fecha de Ingreso: mayo-2003
Ubicación: Panama city, Panama, Panama
Mensajes: 1.154
Antigüedad: 21 años
Puntos: 5
tiene que ser obligatoriamente con persist.upload??
tengo uno que hace lo que pides... si te interesa me avisas.
__________________
Saruman

One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them.
  #3 (permalink)  
Antiguo 16/09/2004, 13:23
Avatar de Saruman  
Fecha de Ingreso: mayo-2003
Ubicación: Panama city, Panama, Panama
Mensajes: 1.154
Antigüedad: 21 años
Puntos: 5
aqui esta el codigo.
no lo he probado, pero este script me funciona perfectamente....
lo adapte mas o menos a los campos que tenias....

me avisas como sale el test.


upload.asp

Código:
<!--#include file="loader.asp" -->

<%
	server.ScriptTimeout = 100000000
	
	Set Master = Server.CreateObject("ADODB.Connection")
	Master.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("../base/cybernetsc.mdb"))
	
	path = "../noticias/images/"
	
	Dim Uploader, File
	Set Uploader = New FileUploader
	
	Uploader.Upload()

	If Uploader.Files.Count <> 0 Then
		titulo = Uploader.form("txttitulo")
		noticia = Uploader.form("txtnoticia")
		archivo = Uploader.form("txtarchivo")
		archivo = replace(archivo, "/", "\")
		archivo = right(archivo, len(archivo) - instrrev(archivo, "\"))
		archivo = lcase(archivo)
		archivo = replace(archivo, " ", "_")
		
		For Each File In Uploader.Files.Items
			File.SaveToDisk Server.MapPath(path), archivo
		Next
		
		sSQL = "insert into noticias(imagen,titulo,noticia) values ('" & archivo & "', '" & titulo & "', '" & noticia & "')"
		Master.Execute(sSQL)
		
		response.Redirect("confirma.htm")
	end if
%>

<script language="javascript">
	function get_archivo() {
		var f = document.form1
		
		f.txtarchivo.value = f.file.value;
		return true
	}
</script>

<form action="upload.asp" method="post" enctype="multipart/form-data" name="form1" onSubmit="return get_archivo()">
  <table width="400" border="1">
    <tr>
      <td width="49">titulo</td>
      <td width="335"><input name="textfield" type="text" size="30"></td>
    </tr>
    <tr>
      <td>foto</td>
      <td><input name="file" type="file" size="30">
      <input name="txtarchivo" type="hidden"></td>
    </tr>
    <tr>
      <td>noticia</td>
      <td><textarea name="textarea" cols="40"></textarea></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><input type="submit" name="Submit" value="grabar"></td>
    </tr>
  </table>
</form>

loader.asp

Código:
<%
Class FileUploader
	Public  Files
	Private mcolFormElem

	Private Sub Class_Initialize()
		Set Files = Server.CreateObject("Scripting.Dictionary")
		Set mcolFormElem = Server.CreateObject("Scripting.Dictionary")
	End Sub
	
	Private Sub Class_Terminate()
		If IsObject(Files) Then
			Files.RemoveAll()
			Set Files = Nothing
		End If
		If IsObject(mcolFormElem) Then
			mcolFormElem.RemoveAll()
			Set mcolFormElem = Nothing
		End If
	End Sub

	Public Property Get Form(sIndex)
		Form = ""
		If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
	End Property

	Public Default Sub Upload()
		Dim biData, sInputName
		Dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos
		Dim nPosFile, nPosBound

		biData = Request.BinaryRead(Request.TotalBytes)
		nPosBegin = 1
		nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
		
		If (nPosEnd-nPosBegin) <= 0 Then Exit Sub
		 
		vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
		nDataBoundPos = InstrB(1, biData, vDataBounds)
		
		Do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString("--"))
			
			nPos = InstrB(nDataBoundPos, biData, CByteString("Content-Disposition"))
			nPos = InstrB(nPos, biData, CByteString("name="))
			nPosBegin = nPos + 6
			nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
			sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
			nPosFile = InstrB(nDataBoundPos, biData, CByteString("filename="))
			nPosBound = InstrB(nPosEnd, biData, vDataBounds)
			
			If nPosFile <> 0 And  nPosFile < nPosBound Then
				Dim oUploadFile, sFileName
				Set oUploadFile = New UploadedFile
				
				nPosBegin = nPosFile + 10
				nPosEnd =  InstrB(nPosBegin, biData, CByteString(Chr(34)))
				sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
				oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, "\"))

				nPos = InstrB(nPosEnd, biData, CByteString("Content-Type:"))
				nPosBegin = nPos + 14
				nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
				
				oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
				
				nPosBegin = nPosEnd+4
				nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
				oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
				
				If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile
			Else
				nPos = InstrB(nPos, biData, CByteString(Chr(13)))
				nPosBegin = nPos + 4
				nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
				If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
			End If

			nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds)
		Loop
	End Sub

	'String to byte string conversion
	Private Function CByteString(sString)
		Dim nIndex
		For nIndex = 1 to Len(sString)
		   CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1)))
		Next
	End Function

	'Byte string to string conversion
	Private Function CWideString(bsString)
		Dim nIndex
		CWideString =""
		For nIndex = 1 to LenB(bsString)
		   CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1))) 
		Next
	End Function
End Class

Class UploadedFile
	Public ContentType
	Public FileName
	Public FileData
	
	Public Property Get FileSize()
		FileSize = LenB(FileData)
	End Property

	Public Sub SaveToDisk(sPath, archivo)
		Dim oFS, oFile
		Dim nIndex
	
		If sPath = "" Or FileName = "" Then Exit Sub
		If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\"
	
		Set oFS = Server.CreateObject("Scripting.FileSystemObject")
		If Not oFS.FolderExists(sPath) Then Exit Sub
		
		'Set oFile = oFS.CreateTextFile(sPath & FileName, True)
		Set oFile = oFS.CreateTextFile(sPath & archivo, True)
		
		For nIndex = 1 to LenB(FileData)
		    oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
		Next

		oFile.Close
	End Sub
	
	Public Sub SaveToDatabase(ByRef oField)
		If LenB(FileData) = 0 Then Exit Sub
		
		If IsObject(oField) Then
			oField.AppendChunk FileData
		End If
	End Sub

End Class
%>

cualquier comentario sabes que estoy a la orden.
__________________
Saruman

One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them.
  #4 (permalink)  
Antiguo 16/09/2004, 16:30
Avatar de armandoweb  
Fecha de Ingreso: enero-2003
Ubicación: San Cristobal, Santa Fe, Argentina
Mensajes: 544
Antigüedad: 21 años, 4 meses
Puntos: 7
Saruman:

Gracias por el codigo.
Lo pruebo y te digo.
Gracias por anticipado.

ARIEL CRIPPA
  #5 (permalink)  
Antiguo 16/09/2004, 16:59
Avatar de armandoweb  
Fecha de Ingreso: enero-2003
Ubicación: San Cristobal, Santa Fe, Argentina
Mensajes: 544
Antigüedad: 21 años, 4 meses
Puntos: 7
Saruman:

Quiero decirte que tu codigo funciona!!
He podido subir la imagen al servidor y la noticia a la base de datos!!
Muchisimas gracias...

Saludos desde Argentina.

ARIEL CRIPPA
  #6 (permalink)  
Antiguo 17/09/2004, 09:55
Avatar de Saruman  
Fecha de Ingreso: mayo-2003
Ubicación: Panama city, Panama, Panama
Mensajes: 1.154
Antigüedad: 21 años
Puntos: 5
Ok, me alegro
__________________
Saruman

One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them.
  #7 (permalink)  
Antiguo 29/09/2004, 19:50
Avatar de jrsouto  
Fecha de Ingreso: junio-2003
Ubicación: en una cabina de 2,2 x 2 x 2,2
Mensajes: 112
Antigüedad: 21 años
Puntos: 0
Estoy probando hacer upload por este metodo pero una imagen de 930Kb se me tarda mas de 15 minutos (si lo hago via FTP sube en 22 seg), eso es normal?
__________________
Mientras mas aprendo mas me doy cuenta de lo poco que sé
  #8 (permalink)  
Antiguo 29/09/2004, 20:56
 
Fecha de Ingreso: abril-2004
Ubicación: México D.F.
Mensajes: 1.724
Antigüedad: 20 años, 1 mes
Puntos: 4
Recordar que servidores FTP están destinados al servicio de transferencia de archivos, tal vez mayor ancho de banda en vez de estar escuchando 2000 peticiones que lo haría el web.
  #9 (permalink)  
Antiguo 30/09/2004, 06:45
Avatar de Muzztein  
Fecha de Ingreso: agosto-2002
Ubicación: Hangar 18
Mensajes: 1.703
Antigüedad: 21 años, 9 meses
Puntos: 16
Cita:
Iniciado por jrsouto
Estoy probando hacer upload por este metodo pero una imagen de 930Kb se me tarda mas de 15 minutos (si lo hago via FTP sube en 22 seg), eso es normal?


de ahi que el ftp sea un protocolo especializado en la transferencia de archivos pues.

el http es para otra cosa...

se que a veces es necesario subir archivos mediante una interfaz web, pero para archivos tan pesados no tiene sentido.

  #10 (permalink)  
Antiguo 22/03/2005, 14:38
 
Fecha de Ingreso: marzo-2005
Mensajes: 5
Antigüedad: 19 años, 3 meses
Puntos: 0
Hola!!!
Tengo un problema, yo logro subir las imagenes al servidor pero no puedo identificar que imagen estoy subiendo, es decir imagina un formulario apto para subir 5 archivos donde no todos son obligatorios incluso podria subir solo el ultimo de los 5, el problema es que según si subi el 1 o el 2 etc debo insetar la imagen en tablas distintas, el Uploader.Form funciona para todos los campos del formulario excepto para los de tipo file, no encuentro manera de identificar el upload que estoy haciendo
Alguien puede ayudarme please
ES URGENTE!!!!!!

Última edición por maryury; 22/03/2005 a las 14:40
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 00:08.