<html>
<title>FileUpload Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server">
protected void UploadButton_Click ( object src, EventArgs e ) {
// specify the path on the server to save the uploaded file to.
String savePath = @"c:\tmp\";
// before attempting to perform operations on the file,
// verify that the FileUpload control contains a file
if ( fileUploader.HasFile ) {
// get the name of the file to upload.
String fileName = fileUploader.FileName;
// append the name of the file to upload to the path.
savePath += fileName;
// call the SaveAs method to save the uploaded file to the specified path.
// this example does not perform all the necessary error checking.
// if a file with the same name already exists in the specified path,
// the uploaded file overwrites it.
fileUploader.SaveAs ( savePath );
// notify the user of the name of the file was saved under.
uploadStatusLabel.Text = "Your file was saved as " + fileName;
}
else {
// notify the user that a filename is needed
uploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>FileUpload Example</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<div align="center">
<form id="form1" runat="server">
<p id="msg">Select a file to upload: </p>
<p><asp:fileupload id="fileUploader" runat="server" />
<asp:button id="UploadButton"
text="Upload file"
onclick="UploadButton_Click"
runat="server" /></p>
<p><asp:label id="uploadStatusLabel" runat="server" /></p>
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>