hello all,
Can any please help me in this :

I have a form , in a website which has :

year : text box
Photo : type ="file"
Submit button.

Now, if any user enters the year, and uses the browse button to select any photo, and then clicks submit, i want that particular photo to be uploaded and saved on the my file system ( where the code is present)...Can any1 please help me in this...how to upload an image...

Thanks in advance...

Hi,

It is really simple just to implement the file upload. But depending upon the condition and requirements it varies alot. Let us talk about some conditions that arise with your code.

1) The file uploader must have a file attached with it.
2) Check file size (mustn’t be )
3) The file should be image file only. i.e. It must be of extension Jpg, Jpeg, Gif, Png ....
4) The save file must be realted with particular User or ID (ie, unique to user)
5) Make sure a duplicate file doesn’t exist.
6) Check whether the file is really a image file by opening it.( since, extension can be changed)
7) Create a thumbnail and save it.

I have listed here some common conditions. Now to implment all the condition you can try this.

Public Sub SaveUploadedFile(ByVal postedFile As HttpPostedFile)
		'1) The file uploader must have a file attached with it.
		If postedFile IsNot Nothing Then
			Dim errorTxt As String = String.Empty
			Dim myFile As HttpPostedFile = postedFile
			'2) Check file size (mustn’t be )
			Dim nFileLen As Integer = myFile.ContentLength
			If nFileLen = 0 Then
				errorTxt = "There wasn't any file uploaded."
			End If
			' 3) The file should be image file only. i.e. It must be of extension Jpg, Jpeg, Gif, Png .... ( Consider JPG only in this case)
			If System.IO.Path.GetExtension(myFile.FileName).ToLower() <> ".jpg" Then
				errorTxt = "The file must have an extension of JPG"
			End If
			If errorTxt = String.Empty Then
				'4) The save file must be realted with particular User or ID (ie, unique to user)
				Dim currentUserID As Integer = 1 'GetUserID or any uniqueID
				Dim directoryPath As String = "IMAGE/Folder_" & currentUserID
				If Directory.Exists(Server.MapPath(directoryPath)) Then
					directoryPath = directoryPath & "/"
					' Read file into a data stream
					Dim myData As Byte() = New [Byte](nFileLen - 1) {}
					myFile.InputStream.Read(myData, 0, nFileLen)
					'5) Make sure a duplicate file doesn’t exist.
					Dim sFilename As String = System.IO.Path.GetFileName(myFile.FileName).Replace(" ", "").ToLower()
					Dim file_append As Integer = 0
					While System.IO.File.Exists(Server.MapPath(directoryPath + sFilename))
						file_append += 1
						sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + file_append.ToString() & ".jpg"
					End While
					' Save the stream to disk
					Dim newFile As New System.IO.FileStream(Server.MapPath(directoryPath + sFilename), System.IO.FileMode.Create)
					newFile.Write(myData, 0, myData.Length)
					newFile.Close()
					'6) Check whether the file is really a image file by opening it.( since, extension can be changed)
					Dim myCallBack As New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
					Dim myBitmap As Bitmap
					Try
						myBitmap = New Bitmap(Server.MapPath(directoryPath + sFilename))
						'7) Create a thumbnail and save it.
						file_append = 0
						Dim sThumbExtension As String = "_Thumb"
						Dim sThumbFile As String = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName).Replace(" ", "") + sThumbExtension & ".jpg"
						If System.IO.File.Exists(Server.MapPath(directoryPath + sThumbFile)) Then
							File.Delete(Server.MapPath(directoryPath + sThumbFile))
						End If
						Dim intThumbWidth As Integer = 160
						Dim intThumbHeight As Integer = 120
						' Save thumbnail and output it onto the webpage
						Dim myThumbnail As System.Drawing.Image = myBitmap.GetThumbnailImage(intThumbWidth, intThumbHeight, myCallBack, IntPtr.Zero)
						myThumbnail.Save(Server.MapPath(directoryPath + sThumbFile))
						' Destroy objects
						myThumbnail.Dispose()
						myBitmap.Dispose()

					Catch errArgument As ArgumentException
						System.IO.File.Delete(Server.MapPath(directoryPath & sFilename))
					End Try
				End If
			Else
				ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, "", "alert(" & errorTxt & ");", True)
			End If
		End If
	End Sub
Public Function ThumbnailCallback() As Boolean
        Return False
    End Function

I'm sure this will work for you.:)
Also don't forget to mark it as solved when you have your answer. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.