|
Monday, 09 March 2009
|
|
HTML clipboard
MyFile.PostedFile.SaveAs(Server.MapPath("./") + strFileName);
There are two points worth notice:
1) Remember to set the file permission on your server to allow write for
IUSR_ServerName account.
2) Using the SaveAs()
part is put into a try block so as to handle whatever problem that may encounter
during the upload process, like disk permission and disk space limitation.
Here are the complete code:
<%@ Page Language="C#" Debug="true" %>
<script language="C#" runat="server">
private void btnUpload_Click(Object sender, EventArgs e) {
string strFileName = MyFile.PostedFile.FileName;
strFileName = System.IO.Path.GetFileName(strFileName);
MyFile.PostedFile.SaveAs(Server.MapPath("./") + strFileName);
lblMessage.Text = "Your file: " + strFileName + " has been uploaded successfully !";
}
</script>
<html>
<body>
<h2>ASP.NET Upload using C#</h2>
<form EncType="Multipart/Form-Data" method="post" runat="server" ID="Form2">
<input type="file" id="File1" runat="server" NAME="File1"/>
<asp:Button id="btnUpload" OnClick="btnUpload_Click" Text="Upload!" runat="server" />
<asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>
|