<form id="formFileUpload" method="post" enctype="Multipart/Form-Data" runat="Server">
<b>File Upload In ASP.Net</b>
<hr />
<br />
Choose Your File To Upload : <br />
<input id="FileField" type="File" runat="server" size="60" /><br />
<br />
<asp:Button ID="UploadButton" runat="server" Text="Go"
onclick="UploadButton_Click" /><br />
<div id="UploadDetails" visible="false" runat="server">
File Name: <span id="FileName" runat="server"/> <br />
File Content: <span id="FileContent" runat="server"/><br />
File Size: <span id="FileSize" runat="server"/>bytes<br />
</div>
<span ID="Span1" style="Color:Red" runat="Server"/>
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
string UpPath;
UpPath = "C:\\UploadedUserFiles";
if (! Directory.Exists(UpPath))
{
Directory.CreateDirectory("C:\\UploadedUserFiles\\");
}
}
protected void UploadButton_Click(object sender, EventArgs e)
{
FileName.InnerHtml = FileField.PostedFile.FileName;
FileContent.InnerHtml = FileField.PostedFile.ContentType;
FileSize.InnerHtml = FileField.PostedFile.ContentLength.ToString();
UploadDetails.Visible = true;
string strFileName;
strFileName = FileField.PostedFile.FileName;
string c = System.IO.Path.GetFileName(strFileName);
try
{
FileField.PostedFile.SaveAs("C:\\UploadedUserFiles\\" + c);
Span1.InnerHtml = "File Uploaded Sucessfully.";
}
catch(Exception exp)
{
Span1.InnerHtml = "Some Error occured.";
UploadDetails.Visible = false;
}
}
Source ASP.NET Videos
No comments:
Post a Comment