TThe FileUpload control supports the following properties :
Sr.No | Property | Description |
1) | ID | Set the uniqe value to control to access in code-behind code. |
2) | Enabled | This property used to control enable or disable the FileUpload control. |
3) | FileBytes | This property return the uploaded file contents as a byte array |
4) | FileContent | This property return the uploaded file contents as a stream. |
5) | FileName | This property used to get the name of the file uploaded. |
6) | HasFile | This property used to check file is upload or not. If file is upload then it return true. |
7) | PostedFile | This property is used to get the uploaded file wrapped in the HttpPostedFile object. |
The FileUpload control also supports the following methods:
Sr.No | Method | Description |
1) | Focus | This method is used to set the focus to the FileUpload control. |
2) | SaveAs | This method is used to save the uploaded file to the file system. |
The FileUpload control’s PostedFile property enables we 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.This is not complete list.
Sr.No | Property | Description |
1) | ContentLength | This property is used to get the size of the uploaded file in bytes. |
2) | ContentType | This property is used to get the MIME type of the uploaded file. |
3) | FileName | This property is used to get the name of the uploaded file. |
4) | InputStream | This property is used to retrieve the uploaded file as a stream. |
The HttpPostedFile class also supports the following method:
Sr.No | Method | Description |
1) | SaveAs | This property is used to save the uploaded file to the file system. |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadExm01.aspx.cs"
Inherits="LearnAsp.Net.Control.FileUpload.FileUploadExm01" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Image FileUpload </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblImageFile" Text="Image Files:" AssociatedControlID="fuImage"
runat="server" />
<asp:FileUpload ID="fuImage" runat="server" />
<br />
<br />
<asp:Button ID="btnAdd" Text="Upload Image" OnClick="btnAdd_Click" runat="server" />
<hr />
<asp:DataList ID="dtlstImages" RepeatColumns="3" runat="server">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Name", "~/UploadImages/{0}") %>'
Style="width: 200px" runat="server" />
<br />
<%# Eval("Name") %>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LearnAsp.Net.Control.FileUpload
{
public partial class FileUploadExm01 : System.Web.UI.Page
{
#region "MessageAlert" Show Alert Popup Message
public void MessageAlert(String Msg, String WinLoc)
{
String str = "";
str = "alert('|| " + Msg +< " ||');";
if (WinLoc != "")
str += "window.location = '" + WinLoc + "';";
ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, str, true);
return;
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (fuImage.HasFile)
{
if (CheckFileType(fuImage.FileName))
{
String filePath = "~/UploadImages/" + fuImage.FileName;
fuImage.SaveAs(MapPath(filePath));
}
else
{
MessageAlert("Plz Upload images to server.", "");
fuImage.Focus();
}
}
}
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);
dtlstImages.DataSource = dir.GetFiles();
dtlstImages.DataBind();
}
}
}
No comments:
Post a Comment
Please do not enter any spam link in the comment box.