This way
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Confirm() {
window.onload = function () {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to replace existing file?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
document.getElementById("<%=btnConfirm.ClientID %>").click();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Upload" runat="server" OnClick="UploadFile" />
<asp:Button ID="btnConfirm" runat="server" OnClick="ConfirmReplace" Style="display: none" />
</form>
</body>
</html>
Code
protected void UploadFile(object sender, EventArgs e)
{
string path = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.FileName);
if (File.Exists(path))
{
Session["PostedFile"] = FileUpload1.PostedFile;
ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "Confirm();", true);
}
else
{
FileUpload1.SaveAs(path);
}
}
protected void ConfirmReplace(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
HttpPostedFile postedFile = (Session["PostedFile"] as HttpPostedFile);
string path = Server.MapPath("~/Files/") + Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(path);
}
Session["PostedFile"] = null;
}
Namespace
using System.IO;