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?
<!-- 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";
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
Thanks,
Ritesh
---------------------------------------------
Please do not forget to mark as Answer if my reply is helpful.
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.
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>
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???
Please Don't mark the Question I posted as Answered until I do it.
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
}
Add client-side script to a Button, LinkButton, or ImageButton s client-sideonclickevent
can be accomplished through the use of theOnClientClickproperty
<asp:Button ID="Button" runat="server" OnClientClick="return confirm('Are you certain you want to do what you want to do?');">
</asp:Button>
agrarian
Member
6 Points
22 Posts
Asp.net file upload with overwrite confirmation
Dec 23, 2007 05:17 AM|LINK
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?
asp.net 2.0 FileUpload confirm overwrite
aadreja
Contributor
2090 Points
354 Posts
Re: Asp.net file upload with overwrite confirmation
Dec 23, 2007 07:52 AM|LINK
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
Ritesh
---------------------------------------------
Please do not forget to mark as Answer if my reply is helpful.
agrarian
Member
6 Points
22 Posts
Re: Asp.net file upload with overwrite confirmation
Dec 23, 2007 09:24 AM|LINK
aadreja
Contributor
2090 Points
354 Posts
Re: Asp.net file upload with overwrite confirmation
Dec 23, 2007 03:00 PM|LINK
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!!!
Ritesh
---------------------------------------------
Please do not forget to mark as Answer if my reply is helpful.
tanise
Member
2 Points
1 Post
Re: Asp.net file upload with overwrite confirmation and problems wtih AJAX
Oct 15, 2008 05:02 AM|LINK
Hi,
Neither overwrite nor upload happens when used with ScriptManager and UpdatePanel... Whats the reason and solution behind this..
krishnada25
Member
631 Points
906 Posts
Re: Asp.net file upload with overwrite confirmation
Nov 17, 2008 02:37 AM|LINK
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???
Thanks,
Kris
pradeepmanne
Member
109 Points
103 Posts
Re: Asp.net file upload with overwrite confirmation
Nov 21, 2011 02:10 PM|LINK
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
Pradeep kumar
ramie.net
Member
6 Points
4 Posts
Re: Asp.net file upload with overwrite confirmation
Nov 23, 2011 09:08 AM|LINK
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 }ramie.net
Member
6 Points
4 Posts
Re: Asp.net file upload with overwrite confirmation
Nov 23, 2011 10:55 AM|LINK
you can write it in javascript
Add client-side script to a Button, LinkButton, or ImageButton s client-side
onclickevent can be accomplished through the use of theOnClientClickproperty<asp:Button ID="Button" runat="server" OnClientClick="return confirm('Are you certain you want to do what you want to do?');"> </asp:Button>pradeepmanne
Member
109 Points
103 Posts
Re: Asp.net file upload with overwrite confirmation
Dec 14, 2011 02:02 PM|LINK
hi
i used this code its working
Pradeep kumar