Here is a sample showing how to do that.
aspx file:
<form id="Form1" method="post" runat="server">
Select a file to upload:
<input type="file" id="fileUploadControl" runat="Server">
<input type="submit" id="uploadButton" runat="Server" value="Upload..." OnServerClick="uploadButton_Click">
<br><br>
<asp:label id="messageArea" runat="server"></asp:label>
</form>
aspx.cs file:
protected string SavedFileName
{
get
{
object viewState = this.ViewState["SavedFileName"];
return (viewState == null) ? string.Empty : (string)viewState;
}
set
{
this.ViewState["SavedFileName"] = value;
}
}
protected void uploadButton_Click(Object sender, EventArgs e)
{
this.fileUploadControl.PostedFile.FileName
string uploadFilePath = this.fileUploadControl.PostedFile.FileName;
string [] filePathArray = uploadFilePath.Split('\\');
if ( filePathArray.Length <= 0 )
return;
string fileName = filePathArray[filePathArray.Length-1];
string filePath = this.Server.MapPath("Uploads") + "\\" + fileName;
string tempFilePath = this.Server.MapPath("Temp") + "\\" + fileName;
bool shouldQueryUser = false;
try
{
if ( System.IO.File.Exists(filePath) )
{
shouldQueryUser = true;
filePath = tempFilePath;
}
if ( System.IO.File.Exists(filePath) )
System.IO.File.Delete(filePath);
this.fileUploadControl.PostedFile.SaveAs(filePath);
this.messageArea.Text = "Successful Upload!<br>";
}
catch(Exception ex)
{
this.messageArea.Text = "Error saving file: " + fileUploadControl.Value + "<br>" + ex.ToString() + "<br>";
}
if ( shouldQueryUser )
{
this.SavedFileName = fileName;
System.Text.StringBuilder javaScript = new System.Text.StringBuilder();
javaScript.Append("\n<script type=text/javascript>\n");
javaScript.Append("<!--\n");
javaScript.Append("var overwriteVerification = window.confirm('Do you wish to overwrite?');\n");
javaScript.Append("__doPostBack('FileOverwritePostBack', overwriteVerification);\n");
javaScript.Append("// -->\n");
javaScript.Append("</script>\n");
RegisterStartupScript(scriptKey, javaScript.ToString());
}
}
protected void deleteFile()
{
this.messageArea.Text = string.Empty;
string fileName = this.SavedFileName;
string tempFilePath = this.Server.MapPath("Temp") + "\\" + fileName;
this.SavedFileName = string.Empty;
try
{
if ( System.IO.File.Exists(tempFilePath) )
System.IO.File.Delete(tempFilePath);
}
catch(Exception ex)
{
this.messageArea.Text = "Error deleting file: " + fileName + "<br>" + ex.ToString() + "<br>";
}
}
protected void moveFile()
{
this.messageArea.Text = string.Empty;
string fileName = this.SavedFileName;
string filePath = this.Server.MapPath("Uploads") + "\\" + fileName;
string tempFilePath = this.Server.MapPath("Temp") + "\\" + fileName;
this.SavedFileName = string.Empty;
try
{
if ( System.IO.File.Exists(filePath) )
System.IO.File.Delete(filePath);
// Move the file.
System.IO.File.Move(tempFilePath, filePath);
}
catch(Exception ex)
{
this.messageArea.Text = "Error moving file: " + fileName + "<br>" + ex.ToString() + "<br>";
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Insure that the __doPostBack() JavaScript is added to the page...
this.GetPostBackEventReference(this, string.Empty);
if ( this.IsPostBack )
{
string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
if ( eventTarget = "FileOverwritePostBack" )
{
if ( eventArgument == "true" )
this.moveFile();
else
this.deleteFile();
}
}
}
Note that RegisterOnSubmitStatement, RegisterStartupScript, RegisterClientScriptBlock, etc have changed since version 1.1 and you will get a compiler warning with the above.
See http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx for more info.
NC...