Friday, September 7, 2018

Replace DUPLICATE FILE USING FILE UPLOAD CONTROL


    void SaveFile(HttpPostedFile file)
    {




        // Specify the path to save the uploaded file to.
        string savePath = Server.MapPath(@"~/FOLDER/") + "//facphoto//";

        //  string savePath = Server.MapPath("~") + "//upload//";

        // 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. 

            Label6.Text = "A file with the same name already exists." + "<br />Your file was saved as " + fileName;
            Label5.Text = fileName;
            Image1.ImageUrl = "/FOLDER/facphoto/" + Label5.Text;



        }
        else
        {
            // Notify the user that the file was saved successfully.
            Label6.Text = "Your file was uploaded successfully.";
            Label5.Text = fileName;
            Image1.ImageUrl = "/FOLDER/facphoto/" + Label5.Text;
        }


        // 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);


    }

No comments:

Post a Comment

GRIDVIEW ON ROW DATA BOUND EVENT

 Database Create  Student : roll , name , city , cost  Fix 6 Value  in Database Record  ====================================================...