...

View Full Version : Image upload - Filename stored in DB



holty
02-02-2003, 12:48 PM
Hi,

I'm having a few probs, I've been looking around for an ASP script that will upload in image into the /images folder and store the filename and extension in the database. Has anyone done this before? Any help would be great.

Ta

angiras
02-04-2003, 07:48 PM
I have stop working with asp since 2 years
here is the asp net (vb net code) as class, if it can help

you can upload as many images as you want at the same time, limit the size and the extension

to get the name is included in , you hjust get this name and extension and do a normal insert into your database



#Region " imports "

Imports mEncoder = System.Drawing.Imaging.Encoder

#End Region

Public Class Telech

#Region " variables "

Private _Type As String

Private _Extension As String

Private _Gimage, _Pimage As Double
Private _Miniature As Boolean ' on genere ou non une miniature

Private _Increment As Boolean
Private _NomImage As String

Private _DossImages As String

Private _ImageGenre As String ' G, D, N

Private _ImageFichier As HtmlInputFile

Private _Multiplicateur As Int32

#End Region

#Region " constructeurs "

Sub New()
MyBase.new()

_DossImages = "produit"
_Miniature = True ' on genere une miniature
_Pimage = Convert.ToDouble(webConfig.TailleImages(0))
_Gimage = Convert.ToDouble(webConfig.TailleImages(1))

_ImageGenre = "G"
_Multiplicateur = 120

If (HttpContext.Current.Request.Browser.Browser.Equals("IE")) Then
_Type = "image/pjpeg"
Else
_Type = "image/jpeg"
End If

_Extension = ".jpg"

_Increment = False

End Sub

Sub New(ByVal initImageFichier As HtmlInputFile, ByVal initNomImage As String, Optional ByVal initImageGenre As String = "G", Optional ByVal initMiniature As Boolean = True, Optional ByVal initGimage As Double = 520, Optional ByVal initPimage As Double = 60, Optional ByVal initMultiplicateur As Int32 = 120)

MyBase.new()
ImageFichier = initImageFichier
NomImage = initNomImage
ImageGenre = initImageGenre
Miniature = initMiniature
Gimage = initGimage
Pimage = initPimage
Multiplicateur = initMultiplicateur

_DossImages = "Produit"
If (HttpContext.Current.Request.Browser.Browser.Equals("IE")) Then
_Type = "image/pjpeg"
Else
_Type = "image/jpeg"
End If
_Extension = ".jpg"

_Increment = False

End Sub

#End Region

#Region " proprietes "

Public Property ImageFichier() As HtmlInputFile
Get
Return _ImageFichier
End Get
Set(ByVal Value As HtmlInputFile)
_ImageFichier = Value
End Set
End Property

Public Property ImageGenre() As String
Get
Return _ImageGenre
End Get
Set(ByVal Value As String)
_ImageGenre = Value
End Set
End Property

Public Property Miniature() As Boolean
Get
Return _Miniature
End Get
Set(ByVal Value As Boolean)
_Miniature = Value
End Set
End Property

Public Property DossImages() As String

Get
Return _DossImages
End Get

Set(ByVal Value As String)
_DossImages = Value
End Set

End Property

Public ReadOnly Property DossierFinal() As String
Get
Return (Localiser.Racine + "Images\" + DossImages + "\" + ImageGenre + "\")
End Get
End Property

Public ReadOnly Property DossierMiniature() As String
Get
Return (Localiser.Racine + "Images\" + DossImages + "\P\")
End Get
End Property

Public Property Type() As String

Get
Return _Type
End Get

Set(ByVal Value As String)
_Type = Value
End Set

End Property

Public Property Extension() As String

Get
Return _Extension
End Get

Set(ByVal Value As String)
_Extension = Value
End Set

End Property

Public ReadOnly Property MaxTaille() As Int32

Get
Return CType(Gimage * Multiplicateur, Int32)
End Get


End Property

Public Property Multiplicateur() As Int32

Get
Return _Multiplicateur
End Get

Set(ByVal Value As Int32)
_Multiplicateur = Value
End Set

End Property

Public Property Gimage() As Double
Get
Return _Gimage
End Get

Set(ByVal Value As Double)
_Gimage = Value
End Set

End Property

Public Property Pimage() As Double

Get
Return _Pimage
End Get

Set(ByVal Value As Double)
_Pimage = Value
End Set

End Property

Public Property Increment() As Boolean

Get
Return _Increment
End Get

Set(ByVal Value As Boolean)
_Increment = Value
End Set

End Property

Public Property NomImage() As String

Get
Return _NomImage
End Get

Set(ByVal Value As String)
_NomImage = Value
End Set

End Property

#End Region

#Region " methodes "

Public Sub Upload()

Dim factorG, factorP As Double

Dim _Nom As String = ImageFichier.PostedFile.FileName
Dim _Contenu As String = ImageFichier.PostedFile.ContentType
Dim _Taille As String = ImageFichier.PostedFile.ContentLength.ToString

Dim c1 As String = Path.GetFileName(_Nom)

Dim sInc As String

If Increment Then
sInc = "_1"
End If

If _Contenu <> Type Then
Throw New YsException("ImageErrFormat" + Extension, 1)
Exit Sub
End If

If ImageFichier.PostedFile.ContentLength > MaxTaille Then
Throw New YsException("ImageErrTaille" + Convert.ToString(MaxTaille), 2)
Exit Sub
End If

If _Contenu = Type And ImageFichier.PostedFile.ContentLength <= MaxTaille Then

Try
Dim mStream As IO.Stream = ImageFichier.PostedFile.InputStream
Dim imgIn As System.Drawing.Bitmap = New System.Drawing.Bitmap(mStream)
Dim x As Int32 = CType(imgIn.Height, Int32)
Dim y As Int32 = CType(imgIn.Width, Int32)

If y <> Gimage Then ' si l'image fait pas la largeur voulue on redimensionne

If x >= y Then
factorG = CType((Gimage / x), Double)
If Miniature Then
factorP = CType((Pimage / x), Double)
End If
Else
factorG = CType((Gimage / y), Double)
If Miniature Then
factorP = CType((Pimage / y), Double)
End If
End If
Try
Dim imgOutG As System.Drawing.Bitmap = New System.Drawing.Bitmap(imgIn, CType(factorG * y, Int32), CType(factorG * x, Int32))
imgOutG.Save((DossierFinal + NomImage + Extension), imgIn.RawFormat)
imgOutG.Dispose()

If Miniature Then
Dim imgOutP As System.Drawing.Bitmap = New System.Drawing.Bitmap(imgIn, CType(factorP * y, Int32), CType(factorP * x, Int32))
imgOutP.Save((DossierMiniature + NomImage + Extension), imgIn.RawFormat)
imgOutP.Dispose()
End If

Catch Exp As Exception
Throw New YsException("ImageErrTaille" + Convert.ToString(MaxTaille) + "ImageErrFormat" + Extension, 3)
End Try
Else
imgIn.Save((DossierFinal + NomImage + Extension), imgIn.RawFormat)

If Miniature Then
If x >= y Then
factorP = CType((Pimage / x), Double)
Else
factorP = CType((Pimage / y), Double)
End If

Dim imgOutP As System.Drawing.Bitmap = New System.Drawing.Bitmap(imgIn, CType(factorP * y, Int32), CType(factorP * x, Int32))

imgOutP.Save((DossierMiniature + NomImage + Extension), imgIn.RawFormat)
imgOutP.Dispose()
End If

End If

imgIn.Dispose()

Catch Exp As Exception
Throw New YsException("ImageErrTaille" + Convert.ToString(MaxTaille) + "ImageErrFormat" + Extension, 4)
Exit Sub
End Try
Else

Throw New YsException("ImageErrTaille" + Convert.ToString(MaxTaille) + "ImageErrFormat" + Extension, 5)
Exit Sub

End If


End Sub

#End Region

End Class

holty
02-06-2003, 07:13 PM
Thanks for that mate, I haven't studied vb.net but will be looking into it soon.

Anyone got an upload script as mentioned in my first post?

Leeus
02-09-2003, 09:54 PM
Here ya go mate, this is just what you are looking for, it might need a little butchering as this one adds more comments into a DB not just file name.

This is also good as it replaces spaces in the filename with %20 which is a problem I came across.


<%


Dim upl, NewFileName

Set upl = Server.CreateObject("ASPSimpleUpload.Upload")

If Len(upl.Form("File1")) > 0 Then
NewFileName = "/support/uploads/" & session("logupload") & "/" & upl.ExtractFileName(upl.Form("File1"))
If upl.SaveToWeb("File1", NewFileName) Then
Response.Write("File successfully written to disk.")
Else
Response.Write("There was an error saving the file to disk.")
End If
End If

'Declare Variables
Dim Username, strText, newStr, intwhere, StrLenTxt, rightchars, leftchars

'Assign variables
urlString= upl.ExtractFileName(upl.Form("File1")) ' can be modified to suit a querystring
strText=urlString
intwhere=0

'First check
intwhere=(InStr(strText, " "))
if intwhere=0 then
newStr=strText
else
strLenTxt=Len(strText)
rightchars=strLenTxt-intwhere
intwhere=intwhere-1
strLeftChars = Left(strText, intwhere)
strRightChars = Right(strText, rightchars)
newStr=strLeftChars & "%20" & strRightChars & ""
end if

do while NOT intwhere=0
'Second check
intwhere=(InStr(newStr, " "))
if intwhere=0 then
else
strLenTxt=Len(newStr)
rightchars=strLenTxt-intwhere
intwhere=intwhere-1
strLeftChars = Left(newStr, intwhere)
strRightChars = Right(newStr, rightchars)
newStr=strLeftChars & "%20" & strRightChars & ""
end if

loop



varuser = session("user")
vardate = now()
varlog = session("logupload")
commentstr = "File " & "<A HREF=http://IISENTRY/support/uploads/" & session("logupload") & "/" & newstr & "><Font color=#ff0000><B>&quot;" & upl.ExtractFileName(upl.Form("File1")) & "&quot;</b></FONT></A>"
commentstr = commentstr & " attached by " & varuser
'commentstr = commentstr & ", Please click on the following " & "<A HREF=http://IISENTRY/support/uploads/" & session("logupload") & "/" & newstr & ">" & upl.ExtractFileName(upl.Form("File1")) & "</A> for the file"
response.write(commentstr)
set objDBconn = server.CreateObject("ADODB.Command")
objDBconn.ActiveConnection = "dsn=SupportDB;"
objDBconn.CommandText = "Insert Into dbo.SupportAudit (UserID, CommentDate, Comment, LogNumber) Values ('" & varuser & "', '" & vardate & "', '" & commentstr & "', '" & varlog & "')"
objDBconn.Execute
objDBconn.ActiveConnection.Close
pageto = "default.asp?User="
pageto = pageto & session("user") & "&LogNumber="
pageto = pageto & session("logupload")
'response.write(pageto)
response.Redirect(pageto)
%>

holty
02-09-2003, 10:51 PM
Leeus ,

Cheers for that, i will have a butchering session over the next few days and tell you how i get on! Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum