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...

Dani AI

Generated

Good foundation from and a useful example from that demonstrates the upload flow and thumbnailing. The VB sample already addresses many validation concerns; the notes below focus on deployment, security, and reliability issues that often break uploads once you move from testing to a real server.

Quick form + server limits to remember

<form method="post" enctype="multipart/form-data">
  <input type="text" name="year" />
  <input type="file" name="photo" />
  <button type="submit">Upload</button>
</form>
<system.web>
  <httpRuntime maxRequestLength="10240" /> <!-- 10 MB -->
</system.web>
<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="10485760" /> <!-- 10 MB -->
    </requestFiltering>
  </security>
</system.webServer>

Practical deployment and security tips

  • Ensure the upload folder is writable by the application pool identity only; do not grant broad write rights.
  • Store uploaded files outside the webroot or prevent script execution in the upload folder to avoid remote code risks. Serve files through a safe handler that enforces access rules.
  • Use a collision-free naming strategy (for example a GUID plus original extension) and keep the original filename/year/user mapping in a database for lookups.
  • Avoid loading large files fully into memory; stream to disk or use the framework SaveAs/stream copy APIs to keep memory usage predictable.
  • For image processing on modern runtimes, prefer maintained libraries (ImageSharp, SkiaSharp) rather than depending on System.Drawing on non-Windows platforms.
  • In production, queue CPU work (thumbnailing, virus scanning) so uploads return quickly and processing is resilient.

Troubleshooting pointers

  • If uploads result in empty files, check that the form uses POST and multipart/form-data.
  • If you get 413 or request-size errors, adjust web.config/IIS limits.
  • If writes fail, verify the app pool identity and folder ACLs and check the server event/logs.

These additions complement ’s sample and help avoid common server-side pitfalls when moving an upload feature into production.

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.