Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone...
i have a folder name called ProductImage..i should save the image into this folder..anyone please help me...i am new to asp.net


here is the code
C#
public void ProcessRequest(HttpContext context)
{
 HttpPostedFile file = context.Request.Files[0];
 file.SaveAs(context.Server.MapPath() + "\" + file.FileName);
 }



what should i give inside the server.mappath()??
Posted
Updated 10-Jul-14 1:19am
v2
Comments
CHill60 10-Jul-14 7:10am    
What have you tried and where are you stuck?

protected void  UploadButton_Click(object sender, EventArgs e)
 {
     // Before attempting to save the file, verify
     // that the FileUpload control contains a file.
     if (FileUpload1.HasFile)
       // Call a helper method routine to save the file.
       SaveFile(FileUpload1.PostedFile);
     else
       // Notify the user that a file was not uploaded.
       UploadStatusLabel.Text = "You did not specify a file to upload.";
 }

   void SaveFile(HttpPostedFile file)
   {
     // Specify the path to save the uploaded file to.
     string savePath = "c:\\temp\\uploads\\";

     // Get the name of the file to upload.
     string fileName = FileUpload1.FileName;

     // Create the path and file name to check for duplicates.
     string pathToCheck = savePath + fileName;

     // Create a temporary file name to use for checking duplicates.
     string tempfileName = "";

     // Check to see if a file already exists with the
     // same name as the file to upload.
     if (System.IO.File.Exists(pathToCheck))
     {
       int counter = 2;
       while (System.IO.File.Exists(pathToCheck))
       {
         // if a file with this name already exists,
         // prefix the filename with a number.
         tempfileName = counter.ToString() + fileName;
         pathToCheck = savePath + tempfileName;
         counter ++;
       }

       fileName = tempfileName;

       // Notify the user that the file name was changed.
       UploadStatusLabel.Text = "A file with the same name already exists." +
           "<br />Your file was saved as " + fileName;
     }
     else
     {
       // Notify the user that the file was saved successfully.
       UploadStatusLabel.Text = "Your file was uploaded successfully.";
     }

     // Append the name of the file to upload to the path.
     savePath += fileName;

     // Call the SaveAs method to save the uploaded
     // file to the specified directory.
     FileUpload1.SaveAs(savePath);

   }
 
Share this answer
 
Comments
Member 10937448 10-Jul-14 7:21am    
thanks sankarsan...it works
logopath = Session["SetCompanyLogo"].ToString();
                                        FileInfo obj = new FileInfo(Server.MapPath(logopath));
                                        obj.CopyTo(Server.MapPath(complogo + Path.GetFileName(logopath)));

                                        objcomp.Logo_Path = complogo + Path.GetFileName(logopath);

                                        if (File.Exists(Server.MapPath(logopath)))
                                        {
                                            File.Delete(Server.MapPath(logopath));
                                        }
 
Share this answer
 
Comments
Member 10937448 10-Jul-14 7:21am    
thanks torakami
XML
Choose Your File  To Upload :
<input id="FileField" type="File" runat="server"/>
 <input id="FileName"  runat="server"/>
<asp:Button ID="UploadButton" runat="server" Text="Go"
    onclick="UploadButton_Click" />



c# code

C#
protected void UploadButton_Click(object sender, EventArgs e)
    {
        FileName.InnerHtml = FileField.PostedFile.FileName;
        string strFileName;
        strFileName = FileField.PostedFile.FileName;
        string c = System.IO.Path.GetFileName(strFileName);
        
        FileField.PostedFile.SaveAs("C:\\UploadedUserFiles\\" + c);
            
    }
 
Share this answer
 
v2
Comments
Member 10937448 25-Jul-14 6:22am    
thanks sagivasan

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