Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone i am stuck in my code.
i am trying to upload a image on my asp.net web page using visual studio 2010.

for just try i used this code

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            FileUpload1.SaveAs(Server.MapPath("~/image/""));
        }

i have image folder on my project like webapplication - image.

but when i try to run the code everytime it gives the same error, could not found the part of path.

C#
FileUpload1.SaveAs(Server.MapPath("~/"));
FileUpload1.SaveAs(Server.MapPath("~"));


i tried all these because i found by checking on internet these different ways, but no one work for me. i check the folder image has a write permission too.
so please can anyone give me solution.
thanks in advance.
Posted
Updated 23-Aug-13 19:05pm
v2
Comments
Dholakiya Ankit 24-Aug-13 1:06am    
what error you are getting put that error here....

If you have your web form and image folder in same directory then try following code

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            FileUpload1.SaveAs(Server.MapPath("image/"+FileUpload1.FileName));
        }


you are missing file name when uploading an image so I added FileUpload1.FileName
 
Share this answer
 
VB
If fu_data.HasFile Then
                    Dim extension As String = Path.GetExtension(fu_data.PostedFile.FileName)
                    Dim filepath As String = "DLR_Data/" & id & extension
                    fu_data.SaveAs(Server.MapPath(filepath))
                    btn_save.Enabled = False
                    lbl_msg.Text = "Data Inserted Successfully"
                End If
 
Share this answer
 
Hi sandeep,
thank you.

i tried this but it dose`t work. i don`t know whats wrong i did.
 
Share this answer
 
Comments
Sandeep Singh Shekhawat 24-Aug-13 0:36am    
What error are you getting? your image folder and web form page in same directory.
Well, I got the solution.
Here it is

C#
FileUpload1.SaveAs(Server.MapPath("~/image/" +FileUpload1.FileName));


Still it doesn`t show me in my application like in visual studio but when I checked by going in c:/user/....directory it was in image folder.

Thank you.
 
Share this answer
 
v2
Comments
Sandeep Singh Shekhawat 24-Aug-13 1:38am    
I am glad to know that your problem has been solved. Do you think that the solution 1 was helpful or not for you?
[no name] 29-Aug-13 21:06pm    
yea, its useful.. thank you for that..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900