Answered by:
Asp.net file upload with overwrite confirmation

Question
-
User-574920078 posted
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?
Sunday, December 23, 2007 1:17 AM
Answers
-
User535736832 posted
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;
}
{
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!!!- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, December 23, 2007 11:00 AM
All replies
-
User535736832 posted
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 queriesSunday, December 23, 2007 3:52 AM -
User-574920078 posted
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.Sunday, December 23, 2007 5:24 AM -
User535736832 posted
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;
}
{
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!!!- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, December 23, 2007 11:00 AM -
User-485743180 posted
Hi,
Neither overwrite nor upload happens when used with ScriptManager and UpdatePanel... Whats the reason and solution behind this..
Wednesday, October 15, 2008 1:02 AM -
User1051635487 posted
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???
Sunday, November 16, 2008 10:37 PM -
User-43865706 posted
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
Monday, November 21, 2011 10:10 AM -
User1138998628 posted
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 5:08 AM -
User1138998628 posted
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 theOnClientClick
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, November 23, 2011 6:55 AM -
User-43865706 posted
hi
i used this code its working
Wednesday, December 14, 2011 10:02 AM