Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading anImage to a folder specific to username 1

Status
Not open for further replies.

autumnEND

Programmer
Nov 1, 2005
61
GB
Hi, ive been trying to enable users to add images to a folder based on there user.identity.name

so if the user is logged in as TEST
when they add an image it would make a folder inside the images folder called TEST

C:\\images\TEST

also would there be a way to check if the folder was allready created? if it was still uplaod the file into the folder

ive been using this code to add files

If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs("C:\\Files\\" + FileUpload1.FileName)
LblStatus.Text = "File Successfully Uploaded"
Catch
LblStatus.Text = "Unable to save the file"
End Try
Else
LblStatus.Text = "You have to select a file to upload"
End If


also is it possible to set allowable extensions to such as .jpg, .bmp etc?

im new to asp.net , so any help would be greatly appreciated
 
Hi,

Is the web application running on intranet or internet?
 
Internet..., so:

The path C:\\files, etc is not right. It will attempt to upload it on the client's pc. You will have an exception for that (IO permition, security).

For that:

I assume that you have an "Images" folder on the root of the web site. To upload use something like: FileUpload1.SaveAs(server.mappath("images") & "/" & user.identity.name & "\" & FileUpload1.FileName

To check if the folder exist, simply do: if io.directory.exists(server.mappath("images") & "/" & user.identity.name) = false then create it

Forgot anything?
 
Oops, "\" should be "/" but doe not matter + insert a missing ) right after .FileName
 
oh right i see what you mean.
i managed to get it working by using

System.IO.Directory.CreateDirectory("C:\\files\\" + User.Identity.Name + "\\")

under the button click pocedure.

but ill use your method

the only thing now is to set allowable extensions, so users can only add image base extensions etc

thanks for the help so far :)
 
Yes, but when you deploy it to a remote server and not your pc, there will be problem with that path. Use instead dynamic path (virtual path) using server.mappath

To make sure that it is an image check: FileUpload1.PostedFile.ContentType

and a small correction.. button click event
 
If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs(Server.MapPath("images") & "/" & User.Identity.Name & "/" & FileUpload1.FileName)
LblStatus.Text = "File Successfully Uploaded"
Catch
LblStatus.Text = "Unable to save the file"
End Try
Else
LblStatus.Text = "You have to select a file to upload"
End If

i tried this , which is uploading the file to the images directory but isnt creating the folder for the user name :/
 
Does the user fill in his user name and password? And also, have you set the authentication mode to forms ? (in web.config)
 
yep the user logs in with a user name and password.

and i have <authentication mode="Forms"/> set in web config
 
Listen,

you must have problem with the user credential validation. If you do not validate him and get his name, then the user.identity.name will be empty. So the image will be saved at:
Server.MapPath("images") & "/" & User.Identity.Name & "/" & FileUpload1.FileName
which is :
Server.MapPath("images") & "//" & FileUpload1.FileName

As you see it will be uploaded with no errors at the '/images'.

If you are NOT using the new membership controls at ASP.NET 2.0, make sure that after checking the credentials that you give the user an authorization cookie.

When you deal with it, make sure that the folder path exist -> else Exception. You know..:
If NOT io.directory.exists(Server.MapPath("images") & "/" & User.Identity.Name) then io.directory.create(Server.MapPath("images") & "/" & User.Identity.Name)
 
Hi, may thanks its working now , heres the code i used

If Not IO.Directory.Exists(Server.MapPath("images") & "/" & User.Identity.Name) Then
System.IO.Directory.CreateDirectory(Server.MapPath("images") & "/" & User.Identity.Name)

End If

If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs(Server.MapPath("images") & "/" & User.Identity.Name & "/" & FileUpload1.FileName)

LblStatus.Text = "File Successfully Uploaded"
Catch
LblStatus.Text = "Unable to save the file"
End Try
Else
LblStatus.Text = "You have to select a file to upload"
End If

thanks again for your help :)
 
Thanks for the star,

by the way what version of asp.net are you using?
 
now that they are situated in a folder on a server

is there an easy way to retreive them, and display them

in each user folder :images/"user_name" there might be several pictures

and im wanting to display them all

 
Are you wanting to display them in a list or in thumbnails?

Hint: The first option is much easier than the second!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
In that case, you can simply use the System.IO.Directory.GetFiles() method and display them however you want (for example, in a TreeView control).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I was thinking of using a datarepeater control, converted to a template and adding to it an image control. Anyway a treeview works too. An example could be:
Code:
dim tn as treenode
dim myimages() as string=io.directory.getfiles(Server.MapPath("images") & "/" & User.Identity.Name)

for i as integer=0 to myimages.length-1
  tn=new treenode
  tn.imageurl=myimages(i)
  treeview1.nodes.add(tn)
next

tn=nothing
 
hi, thanks for the replies

will the tree view display the actual images then? or just a link to them?

im not sure how to implement the method above, any pointers would be appreciated

also would it be possible to set up so the images were displayed based on a query string?

such as users_page.aspx?UserName=test

would display the images of the user "test"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top