Upload file to server

Rani

Well-known member
Joined
Nov 19, 2005
Messages
71
Programming Experience
Beginner
I have two image folders in my web server saving the actual image file in both folders. one s_folder for thumbnail and the other l_folder for large images. I want to upload images to these folders and make an entry of the file name in database field.

This pc of my code does save in one folder, passes thru the second folder and enters the record in my database. However, when i check the l_folder the image does not exist. I can use another pair of eyes to find out what am missing. That would be awesome. This is the line does not work "Me.Imgfile.PostedFile.SaveAs(str_l_filepath)"

here is my code:
VB.NET:
[SIZE=2]str_s_filepath = str_s_folder & strfilename[/SIZE]
[SIZE=2]str_l_filepath = str_l_folder & strfilename[/SIZE]
[SIZE=2][COLOR=#008000]'strip the type of extn from filename[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] extpos [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = strfilename.LastIndexOf(".")[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ext [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = LCase(strfilename.Substring(extpos))[/SIZE]
 
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] File.Exists(str_s_filepath) [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] File.Exists(str_l_filepath) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblconfirm.Text = strfilename & " already exits on the server!"[/SIZE]
[SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] ext = ".jpg" [/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] ext = ".bmp" [/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] ext = ".gif" [/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] ext = ".jpeg" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Imgfile.PostedFile.SaveAs(str_s_filepath)[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Imgfile.PostedFile.SaveAs(str_l_filepath)[/SIZE]
[SIZE=2][COLOR=#008000]'also update the database fields[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sql [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "Insert into Photo(user_id,photo_date,filename,photo_desc,photo_cat_id)values('" _[/SIZE]
[SIZE=2]& lblowner & "','" & Now.Date & "','" & strfilename & "','" & txt & "','" & ddl & "')"[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] con [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlConnection(Application("connection"))[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(sql, con)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ds [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet[/SIZE]
[SIZE=2]da.Fill(ds)[/SIZE]
Thanks
 
Last edited by a moderator:
try this:
VB.NET:
str_s_filepath = Server.MapPath(str_s_folder & strfilename)
str_l_filepath = Server.MapPath(str_l_folder & strfilename)
 
This is the error i get when i replaced you pc of code.
Invalid path for MapPath 'c:\inetpub\wwwroot\Pictures\s_photos\Var****a.jpg'. A virtual path is expected.

please refer to my previous post.
Just so you know, i do not have problem with my lines of code at that part.
my problem is it saves the image in one folder, it does write a record in database but it does not save in another image folder.

If it can save in one folder and save in db, why it can't save in another folder? That is my question.

Thanks.
 
Could be folder permission for the other folder?
 
Thanks for your time. Please disregard my request. I figured it out.

I was not grabbing the path of the folder properly.
I was doing this:
str_s_folder = "c:\inetpub\wwwroot\Pictures\s_photos\"
str_l_folder = "c:\inetput\wwwroot\Pictures\l_photos\"

Instead of this:
str_s_folder = Request.ServerVariables("appl_physical_path") & "s_photos\"
str_l_folder = Request.ServerVariables("appl_physical_path") & "l_photos\"
 
Back
Top