Share via


Asp.net file upload with overwrite confirmation

Question

Sunday, December 23, 2007 1:17 AM

Hi,

I am using asp:fileupload for uploading files. I want to be able to check if the file already exists in the upload folder and ask the user if he/she wants to overwrite the existing file. How can I accomplish this? 

All replies (9)

Sunday, December 23, 2007 11:00 AM âś…Answered

Ok.. I got it.
Let's use AJAX to solve this.

Javascript
<script type ="text/javascript">
function CheckFileExists()
{
    PageMethods.CheckFileExists(document.getElementById('FileUpload1').value,FileExistsCallBack);
    return false;
}

 

function FileExistsCallBack(result)
{
     if(result) //if file exists on server, confirm with user
     {
        if(confirm("File already exists. Do you want to overwrite"))
             __doPostBack('button1','');
    }
    else
        __doPostBack('button1',''); //file doesn't exists on server.. So upload it
}
</script>

<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true" />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" OnClientClick="return CheckFileExists()" OnClick="button1_click" />

C# Code behind

protected void button1_click(object sender, EventArgs e)
{
    String ServerPath = "C:\";
    FileUpload1.PostedFile.SaveAs(ServerPath + FileUpload1.FileName);
}

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static Boolean CheckFileExists(String FileName)
{
    String ParseFileName;
    ParseFileName = FileName.Substring(FileName.LastIndexOf("\") + 1);
    if (System.IO.File.Exists("C:\" + ParseFileName))
       return true;
    else
        return false;
}

Hope it will be helpful. Well i have tested on my PC and its working as you wanted. enjoy!!!


Sunday, December 23, 2007 3:52 AM

you can achieve this using javascript.
try this

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="button1_click" />
<input type="hidden" runat="server" id="hfFileExists" value="0" />
<input type="hidden" runat="server" id="hfFileExistsConfirm" value="" />
<input type="hidden" runat="server" id="hfPostedFileName" value="" />

<!-- this script should be placed at the end of form -->
<script language="javascript" type="text/javascript">
if(document.getElementById("hfFileExists").value=="1")
{
      if(confirm("File already exists. Do you want to overwrite"))
        document.getElementById("hfFileExistsConfirm").value = "Yes";
     else
       document.getElementById("hfFileExistsConfirm").value = "No";

     document.getElementById("hfFileExists").value="0";
     document.forms[0].submit();
}
</script>

C# Code behind

protected void Page_Load(object sender, EventArgs e)
{
   if (IsPostBack)
   {
      if (hfFileExistsConfirm.Value.ToLower() == "yes")
      {
         hfFileExists.Value = "0";
         hfFileExistsConfirm.Value = "";
         //upload file copy from temp location that we have saved on button click
        System.IO.File.Copy("C:\temp_" + hfPostedFileName.Value, "C:\" + hfPostedFileName.Value, true);
     }
  }
}

protected void button1_click(object sender, EventArgs e)
{
     String ServerPath = "C:\";
     if (System.IO.File.Exists(ServerPath + FileUpload1.FileName))
     {
         hfFileExists.Value = "1";
         hfFileExistsConfirm.Value = "";
         hfPostedFileName.Value = FileUpload1.FileName;
         FileUpload1.PostedFile.SaveAs("C:\temp_" + FileUpload1.FileName); //Save temporary to some other location then if user selects to overwrite file we can copy that file from this location
     }
     else
     {
        //Upload File
         FileUpload1.PostedFile.SaveAs("C:\" + FileUpload1.FileName);
      }
}

Hope it helps, let me know in case of any queries


Sunday, December 23, 2007 5:24 AM

Thanks for the reply. The approach you used requires that the file is still uploaded to a temporary location on the server even if the user later decided to cancel the operation. I would like to be able to prevent that unnecessary overhead.


Wednesday, October 15, 2008 1:02 AM

Hi,

Neither overwrite nor upload happens when used with ScriptManager and UpdatePanel... Whats the reason and solution behind this..


Sunday, November 16, 2008 10:37 PM

Hi Iam using the code that you wrote once I click on OK the file is getting saved in the temporary folder instead of the c:\ folder. how can I copy the existing file from temp to c? and replace the existing one???


Monday, November 21, 2011 10:10 AM

hi,

i have followed the script but its not working on tabpanel

am using ajaxtoolkit for tabpanel the script is not working with tabpanels 

any solution for that???

thanks inadvance


Wednesday, November 23, 2011 5:08 AM

Then check if the file exists before you do the upload.

// Specify the path to save the uploaded file to.
string savePath = "c:\\project\\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;

// Check to see if a file already exists with the same name as the file to upload.        
        if (System.IO.File.Exists(pathToCheck)) 
        {
          //same file exists do something before it is overwritten
        }

 


Wednesday, November 23, 2011 6:55 AM

you can write it in javascript

Add client-side script to a Button, LinkButton, or ImageButton s client-side onclick event can be accomplished through the use of the OnClientClick``property

<asp:Button ID="Button" runat="server" OnClientClick="return confirm('Are you certain you want to do what you want to do?');">
</asp:Button>

Wednesday, December 14, 2011 10:02 AM

hi

i used this  code its working