Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
pls give me code how to upload image in asp.net using c# with code to use file uploder command
Posted
Updated 12-Apr-12 23:37pm
v2
Comments
Shahriar Iqbal Chowdhury/Galib 6-Feb-11 2:07am    
did you tried anything?
What have you tried so far?

C#
protected void UploadThisFile(FileUpload upload)
{
if (upload.HasFile)
{
string theFileName = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName);
if (File.Exists(theFileName))
{
File.Delete(theFileName);
}
upload.SaveAs(theFileName);
}
}


C#
protected void buttonUpload_Click(object sender, System.EventArgs e)
{
UploadThisFile(FileUpload1);

}
 
Share this answer
 
v2
 
Share this answer
 
Comments
Devendra 1988 25-Sep-12 2:55am    
if u can't satisfy then i will provide running code...
Hi ,

Please refer the following Link which will provide solution to your requirement.

File Upload and FileTypes-Extension Check

Hope this will solve the requirement.

Regards,
Mubin
 
Share this answer
 
step1: Add Fileupload tag in your .aspx page:
ASP.NET
<asp:fileupload id="FileUpload1" runat="server" xmlns:asp="#unknown" />

Step2: Now add a button for saving uploaded file
ASP.NET
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Upload" xmlns:asp="#unknown" />

Step3: Add a folder named "Images" in your project

step4: The C# code for image upload is as follows
C#
 protected void Button1_Click(Onject sender, EventArgs e)
{
   string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
   string path = Server.MapPath("/Images"+FileName);
   FileUpload1.SaveAs(path);
}
 
Share this answer
 
Comments
Member 10533447 9-Apr-14 4:13am    
what is path ? or describe path ?
MohanKrishna.Kota 9-Apr-14 4:38am    
It is just a location telling the compiler where to store the selected image
protected void Button1_Click(object sender, EventArgs e)
    {

        string ServerMapPath = Server.MapPath("~//images//"+FileUpload1.FileName);
        
        FileUpload1.PostedFile.SaveAs(ServerMapPath);
    }


Code Behind for button event

<asp:fileupload id="FileUpload1" runat="server" /><br />
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Upload Image:" />


Asp.net webform page
 
Share this answer
 
v3

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