I have a doubt. As I said earlier I am creating a photo gallery for my website. I need to generate thumbnails for the images that I was uploading to the server. And that particular thumbnails should be stored in a particular folder. Could you please let
me know "How do I do so"?
Here's the code that I am using to create a Folder with the subfolder named "Thumbs" and to upload files.
Partial Class FolderCreation
Inherits System.Web.UI.Page
Dim currentDir As String
Dim imgThumb As Image = Nothing
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
foldersList()
End If
End Sub
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim root As String = Server.MapPath("~/" + RadioButtonList1.SelectedItem.Text)
Dim thisPage As String = Request.Path
currentDir = Request.Params("dir")
If currentDir Is Nothing Then
currentDir = root
End If
If Not currentDir.StartsWith(root) Then
currentDir = root
End If
doesFileExist(uploadedFile.FileName)
End Sub
Sub DoUpload()
If Not (uploadedFile.PostedFile Is Nothing) Then
Try
message.Text = ""
Dim postedFile = uploadedFile.PostedFile
Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
postedFile.SaveAs(currentDir & _
directorySeparatorChar.ToString() & filename)
lblStatus.Text = String.Format( _
"Uploaded file: {0}<br/>" & _
"File size (in bytes): {1:N0}<br />" & _
"Content-type: {2}", _
uploadedFile.FileName, _
uploadedFile.FileBytes.Length, _
uploadedFile.PostedFile.ContentType)
Catch ex As Exception
message.Text = "Failed uploading file"
End Try
End If
End Sub
Public Sub doesFileExist(ByVal searchString As String)
If uploadedFile.FileName <> "" Then
Dim filename As String
filename = Server.MapPath(RadioButtonList1.SelectedItem.Text + "\") & searchString.ToString()
If File.Exists(filename) Then
message.Text = "The File which you are trying to upload <b><u>" & searchString & "</b></u> already exists in folder."
lblStatus.Text = ""
Else
DoUpload()
End If
Else
message.Text = ""
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
doesFolderExist(TextBox1.Text)
End Sub
Public Sub doesFolderExist(ByVal searchString As String)
If TextBox1.Text <> "" Then
Dim foldername As String
Dim DirInfo As System.IO.DirectoryInfo
foldername = Server.MapPath("~\" + TextBox1.Text)
If Directory.Exists(foldername) Then
message.Text = "The Event which you are trying to create <b><u>" & searchString & "</b></u> already exists"
lblStatus.Text = ""
Else
DirInfo = System.IO.Directory.CreateDirectory(MapPath("~/" + TextBox1.Text))
DirInfo.CreateSubdirectory("thumbs")
message.Text = "Event " & searchString & " created successfully."
End If
Else
message.Text = ""
End If
foldersList()
End Sub
Sub foldersList()
Dim dirInfo As DirectoryInfo = New DirectoryInfo(MapPath("~/"))
Dim excludedDirs As String() = {"App_Data", "image_gallery", "images", "thumbs", "MAIN", "Background", "backgrounds"}
RadioButtonList1.DataSource = dirInfo.GetDirectories("*", SearchOption.AllDirectories)
RadioButtonList1.DataBind()
For i As Integer = RadioButtonList1.Items.Count - 1 To 0 Step -1
For Each excludedDir As String In excludedDirs
If RadioButtonList1.Items(i).Text = excludedDir Then
RadioButtonList1.Items.RemoveAt(i)
Exit For
End If
Next
Next
End Sub
Protected Sub RadioButtonList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList2.SelectedIndexChanged
If RadioButtonList2.SelectedItem.Text = "Create an Event" Then
MultiView1.ActiveViewIndex = 0
message.Text = ""
lblStatus.Text = ""
ElseIf RadioButtonList2.SelectedItem.Text = "Upload Photos to an Event" Then
MultiView1.ActiveViewIndex = 1
lblStatus.Text = ""
message.Text = ""
End If
End Sub
End Class
Could you please let me know "How do I write code for generating a thumbnail after uploading an image to a particular folder in server"
Note: The generated thumb nail should be stored in "thumbs" folder.
Thanks and Regards
Sandeep.
Please remember to click “Mark as Answer” on the post that helps you.
This can be beneficial to other community members reading the thread
babji.sunny@...
Participant
1218 Points
360 Posts
Re: How to create a forum?
Feb 04, 2009 07:42 AM|LINK
Hi Kipo, thanks for your quick reply.
I have a doubt. As I said earlier I am creating a photo gallery for my website. I need to generate thumbnails for the images that I was uploading to the server. And that particular thumbnails should be stored in a particular folder. Could you please let me know "How do I do so"?
Here's the code that I am using to create a Folder with the subfolder named "Thumbs" and to upload files.
Imports System.IO
Imports System.IO.Directory
Imports System.Drawing
Partial Class FolderCreation
Inherits System.Web.UI.Page
Dim currentDir As String
Dim imgThumb As Image = Nothing
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
foldersList()
End If
End Sub
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim root As String = Server.MapPath("~/" + RadioButtonList1.SelectedItem.Text)
Dim thisPage As String = Request.Path
currentDir = Request.Params("dir")
If currentDir Is Nothing Then
currentDir = root
End If
If Not currentDir.StartsWith(root) Then
currentDir = root
End If
doesFileExist(uploadedFile.FileName)
End Sub
Sub DoUpload()
If Not (uploadedFile.PostedFile Is Nothing) Then
Try
message.Text = ""
Dim postedFile = uploadedFile.PostedFile
Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
postedFile.SaveAs(currentDir & _
directorySeparatorChar.ToString() & filename)
lblStatus.Text = String.Format( _
"Uploaded file: {0}<br/>" & _
"File size (in bytes): {1:N0}<br />" & _
"Content-type: {2}", _
uploadedFile.FileName, _
uploadedFile.FileBytes.Length, _
uploadedFile.PostedFile.ContentType)
Catch ex As Exception
message.Text = "Failed uploading file"
End Try
End If
End Sub
Public Sub doesFileExist(ByVal searchString As String)
If uploadedFile.FileName <> "" Then
Dim filename As String
filename = Server.MapPath(RadioButtonList1.SelectedItem.Text + "\") & searchString.ToString()
If File.Exists(filename) Then
message.Text = "The File which you are trying to upload <b><u>" & searchString & "</b></u> already exists in folder."
lblStatus.Text = ""
Else
DoUpload()
End If
Else
message.Text = ""
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
doesFolderExist(TextBox1.Text)
End Sub
Public Sub doesFolderExist(ByVal searchString As String)
If TextBox1.Text <> "" Then
Dim foldername As String
Dim DirInfo As System.IO.DirectoryInfo
foldername = Server.MapPath("~\" + TextBox1.Text)
If Directory.Exists(foldername) Then
message.Text = "The Event which you are trying to create <b><u>" & searchString & "</b></u> already exists"
lblStatus.Text = ""
Else
DirInfo = System.IO.Directory.CreateDirectory(MapPath("~/" + TextBox1.Text))
DirInfo.CreateSubdirectory("thumbs")
message.Text = "Event " & searchString & " created successfully."
End If
Else
message.Text = ""
End If
foldersList()
End Sub
Sub foldersList()
Dim dirInfo As DirectoryInfo = New DirectoryInfo(MapPath("~/"))
Dim excludedDirs As String() = {"App_Data", "image_gallery", "images", "thumbs", "MAIN", "Background", "backgrounds"}
RadioButtonList1.DataSource = dirInfo.GetDirectories("*", SearchOption.AllDirectories)
RadioButtonList1.DataBind()
For i As Integer = RadioButtonList1.Items.Count - 1 To 0 Step -1
For Each excludedDir As String In excludedDirs
If RadioButtonList1.Items(i).Text = excludedDir Then
RadioButtonList1.Items.RemoveAt(i)
Exit For
End If
Next
Next
End Sub
Protected Sub RadioButtonList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList2.SelectedIndexChanged
If RadioButtonList2.SelectedItem.Text = "Create an Event" Then
MultiView1.ActiveViewIndex = 0
message.Text = ""
lblStatus.Text = ""
ElseIf RadioButtonList2.SelectedItem.Text = "Upload Photos to an Event" Then
MultiView1.ActiveViewIndex = 1
lblStatus.Text = ""
message.Text = ""
End If
End Sub
End Class
Could you please let me know "How do I write code for generating a thumbnail after uploading an image to a particular folder in server"
Note: The generated thumb nail should be stored in "thumbs" folder.
Thanks and Regards
Sandeep.
This can be beneficial to other community members reading the thread