PROWAREtech
ASP.NET: FileUpload Server Control Example
Upload a file using the FileUpload server control for Web Forms (.NET Framework).
This code is a full ASPX page that uses the FileUpload server control. If wanting to upload more than one file, see this article or to upload files using AJAX and MVC see this article.
Note that this will not work with ASP.NET v1.1 because there is no enctype="multipart/form-data" in the <form> element. The enctype is automatically added in versions after v1.1.
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If FileUpload1.HasFile Then
Try
'Make sure directory has write permissions
FileUpload1.SaveAs("C:\" & FileUpload1.FileName)
Catch ex As Exception
Span1.InnerHtml = ex.Message.ToString()
End Try
Else
Span1.InnerHtml = "You have not selected a file."
End If
End Sub
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<p><asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="Button1_Click" /></p>
<p><span ID="Span1" runat="server"></span></p>
</div>
</form>
</body>
</html>
Change the web.config file to allow larger files to be uploaded (maxRequestLength) and to allow more time (executionTimeout) for them to be uploaded.
<system.web>
<httpRuntime executionTimeout="300" maxRequestLength="20480" />
</system.web>
This code will run with ASP.NET v1.1 and is compatible with later versions. Notice the accept attribute being used which is not available to the FileUpload control.
<%@ Page Language="VB" %>
<script runat="server">
Sub Button1_Click(ByVal Sender As Object, ByVal e As EventArgs)
If Not File2.PostedFile Is Nothing Then
If File2.PostedFile.ContentLength > 0 Then
' Posted file
Dim strFileName() As String = File2.PostedFile.FileName.Split("\")
FileName.InnerHtml = strFileName(strFileName.Length - 1)
ContentTyp.InnerHtml = File2.PostedFile.ContentType
FileDetails.Visible = True
Try
' Save uploaded file; make sure write permission is allowed
File2.PostedFile.SaveAs("C:\" & strFileName(strFileName.Length - 1))
Catch ex As Exception
lblError.InnerHtml = ex.Message.ToString()
End Try
End If
End If
End Sub
</script>
<html>
<body>
<form id="form1" enctype="multipart/form-data" runat="server">
Select File To Upload to Server:
<input id="File2" type="file" accept="image/*" runat="server" />
<br /><br />
<input id="Button1" type="submit" value="Upload!" onserverclick="Button1_Click" runat="server" />
<br /><br /><br />
<div id="FileDetails" visible="false" runat="server">
FileName: <span id="FileName" runat="server" /> <br />
ContentType: <span id="ContentTyp" runat="server" /> <br />
</div>
<span id="lblError" runat="server"></span>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
if (File2.PostedFile != null)
{
if (File2.PostedFile.ContentLength > 0)
{
// Posted file
string[] strFileName = File2.PostedFile.FileName.Split('\\');
FileName.InnerHtml = strFileName[strFileName.Length - 1];
ContentTyp.InnerHtml = File2.PostedFile.ContentType;
FileDetails.Visible = true;
try
{
// Save uploaded file; make sure write permission is allowed
File2.PostedFile.SaveAs("C:\\" + strFileName[strFileName.Length - 1]);
}
catch (Exception ex)
{
lblError.InnerHtml = ex.Message.ToString();
}
}
}
}
</script>
<html>
<body>
<form id="form1" enctype="multipart/form-data" runat="server">
Select File To Upload to Server:
<input id="File2" type="file" accept="image/*" runat="server" />
<br /><br />
<input id="Button1" type="submit" value="Upload!" onserverclick="Button1_Click" runat="server" />
<br /><br /><br />
<div id="FileDetails" visible="false" runat="server">
FileName: <span id="FileName" runat="server" /> <br />
ContentType: <span id="ContentTyp" runat="server" /> <br />
</div>
<span id="lblError" runat="server"></span>
</form>
</body>
</html>
Comment