File Uploads in ASP.Net

The FileUpload control enables users to upload files to your web application. After the file is uploaded, you can store the file.

FileUpload control properties :
  • Enabled : Enables you to disable the FileUpload control.
  • FileBytes : Enables you to get the uploaded file contents as a byte array.
  • FileContent : Enables you to get the uploaded file contents as a stream.
  • FileName : Enables you to get the name of the file uploaded.
  • HasFile : Returns True when a file has been uploaded.
  • PostedFile : Enables you to get the uploaded file wrapped in the HttpPostedFile object.
FileUpload control methods:
  • Focus : Enables you to shift the form focus to the FileUpload control.
  • SaveAs : Enables you to save the uploaded file to the file system.
The FileUpload control’s PostedFile property enables you to retrieve the uploaded file wrapped in an HttpPostedFile object. This object exposes additional information about the uploaded file.

The HttpPostedFile class has the following properties :
  • ContentLength : Enables you to get the size of the uploaded file in bytes.
  • ContentType : Enables you to get the MIME type of the uploaded file.
  • FileName : Enables you to get the name of the uploaded file.
  • InputStream : Enables you to retrieve the uploaded file as a stream.
The HttpPostedFile class also supports the following method:
  • SaveAs : Enables you to save the uploaded file to the file system.
File Uploads in ASP.NetYou can save a file by using either the FileUpload.SaveAs() method or the HttpPostedFile.SaveAs() method.

<%@ Page Language=”C#” %>
<%@ Import Namespace=”System.IO” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
protected void btnAdd_Click(object sender, EventArgs e)
{
if (ImageUpload.HasFile)
{
if (CheckFileType(ImageUpload.FileName))
{
String filePath = “~/UploadImages/” + ImageUpload.FileName;
ImageUpload.SaveAs(MapPath(filePath));
}
}
}
bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case “.gif”:
return true;
case “.png”:
return true;
case “.jpg”:
return true;
case “.jpeg”:
return true;
default:
return false;
}
}
void Page_PreRender()
{
string upFolder = MapPath(“~/UploadImages/”);
DirectoryInfo dir = new DirectoryInfo(upFolder);
dlstImages.DataSource = dir.GetFiles();
dlstImages.DataBind();
}
</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>FileUpload File</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:Label id=”lblImageFile” Text=”Image File:” AssociatedControlID=”ImageUpload” Runat=”server” />
<asp:FileUpload id=”ImageUpload” Runat=”server” />
<br /><br />
<asp:Button id=”btnAdd” Text=”Add Image” OnClick=”btnAdd_Click” Runat=”server” />
</div>
</form>
</body>
</html>

The page includes a method named CheckFileType(), which prevents users from uploading a file that does not have the .gif, .jpeg, .jpg, or .png extension. The method restricts the type of file that can be uploaded based on the file extension.
Tags: , , ,
Hot on Web:

About author