Image Uploader ASP.NET control is an ASP.NET server control intended to make deployment of Image Uploader, configuring it, and handling uploaded data easier. It allows ASP.NET developers to utilize Image Uploader in a straight-forward way just as the other ASP.NET server controls and provides the following advantages over a standard way of deployment (this way is described in the Inserting Image Uploader into Web Page topic):
Let us examine Image Uploader ASP.NET control usage by the example of a simple image gallery. Suppose the gallery requires a user to upload downsized copy of image along with a source one. To perform this task we can phase it as follows.
To add Image Uploader ASP.NET control into the gallery page just walk through the following steps:
Create new ASP.NET web site (in Visual Studio 2008 menu File -> New -> Web Site...).
Add Image Uploader ASP.NET control to the toolbox:
Open the page where Image Uploader should be placed in the design mode, then drag and drop the ImageUploader item into the desired position.
There are two ways to configure Image Uploader. The first one is to use the list of control properties in Properties editor. Each of these properties is coupled with a short description, however, if you need more detailed information see Image Uploader ASP.NET Control Reference for it.
The second way is to modify Image Uploader ASP.NET control properties in the source of an ASP.NET page declaratively. See the source code of the main page of our gallery (default.aspx) below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register assembly="Aurigma.ImageUploader" namespace="Aurigma.ImageUploader" tagprefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Image Uploader ASP.NET Control Sample</title> </head> <body> <form id="form1" runat="server" > <cc1:ImageUploader ID="ImageUploader1" runat="server" Height="400" Width="600" Action="Default.aspx" RedirectUrl="Gallery.aspx" UploadThumbnail1FitMode="Fit" UploadThumbnail1Width="120" UploadThumbnail1Height="120" OnFileUploaded="FileUploaded" /> </form> </body> </html>
On this page we have set mandatory parameters, such as Image Uploader identifier and its dimensions, as well as optional parameters required by our image gallery. Here we have specified an event handler which will process uploaded files and a page to which the user will be redirected when the upload successfully completes, set TwoPanes layout as this view is more convenient for the image gallery, and configure one thumbnail for upload. This thumbnail will be created for each image right before upload and will be fitted to 120x120 pixels.
Image Uploader ASP.NET control provides special FileUploaded event to handle uploaded data server-side (the code snippet above demonstrates how it can be specified). This event is fired the same amount of times as a number of files uploaded from the client receiving the data related to a particular file. Here is a source file, thumbnails created for this file client-side, original file name, description and so on. All this data is passed to the event via parameter of FileUploadEventArgs type.
The gallery stores source images and its thumbnails in /Gallery and /Gallery/Thumbnails folders respectively, as it is demonstrated in default.aspx.cs source code below.
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, System.EventArgs e) { } protected void FileUploaded(object sender, Aurigma.ImageUploader.FileUploadEventArgs e) { string galleryPath = "Gallery/"; string thumbnailsPath = galleryPath + "Thumbnails/"; Aurigma.ImageUploader.SourceFileInfo sourceFile = e.SourceFile; Aurigma.ImageUploader.ThumbnailInfo thumbnail = e.Thumbnails[1]; string safeFileName = sourceFile.GetSafeFileName(Server.MapPath(galleryPath)); sourceFile.Save(Server.MapPath(galleryPath + safeFileName)); thumbnail.Save(Server.MapPath(thumbnailsPath + safeFileName)); } }
It solves our task in the following way:
On the configuring step we specified the RedirectUrl
property to the "gallery.aspx"
value. It means that Image Uploader
will redirect users to this page after the upload successfully completes. So, it would be convenient to display uploaded files
here. To implement this functionality we iterate through all the previews stored in the
/Gallery/Thumbnails folder and display each thumbnail as a link to its original image
(source images are stored in the /Gallery folder). See the source code below.
gallery.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Gallery.aspx.cs" Inherits="Gallery" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Gallery</title> </head> <body> <asp:DataList ID="DataList1" Width="100%" RepeatLayout="Table" RepeatDirection="Vertical" RepeatColumns="4" runat="server"> <HeaderTemplate></HeaderTemplate> <ItemTemplate> <a href="<%# galleryPath + System.IO.Path.GetFileName(Container.DataItem.ToString()) %>" target="_blank"> <img src="<%# thumbnailsPath + System.IO.Path.GetFileName(Container.DataItem.ToString()) %>" /> </a> </ItemTemplate> <FooterTemplate></FooterTemplate> </asp:DataList> </body> </html>
gallery.aspx.cs:
public partial class Gallery : System.Web.UI.Page { protected string galleryPath; protected string thumbnailsPath; protected void Page_Load(object sender, System.EventArgs e) { galleryPath = "Gallery/"; thumbnailsPath = galleryPath + "Thumbnails/"; DataList1.DataSource = System.IO.Directory.GetFiles(Server.MapPath(thumbnailsPath)); DataList1.DataBind(); } }
You may run the image gallery application considered in this article on your server. To do it, just paste source code to default.aspx, default.aspx.cs, gallery.aspx, and gallery.aspx.cs files to your server using the following folder structure:
bin Aurigma.ImageUploader.dll Gallery Thumbnails default.aspx default.aspx.cs gallery.aspx gallery.aspx.cs
Make sure that /Gallery and /Gallery/Thumbnails folders have enough permissions to save files to: