Image Gallery and AdRotator

First post, and some of my first real C# coding!

Following a couple of examples in Asp.Net 3.5 Unleashed, I created a form that uploaded images to an image gallery which was displayed on the same page. To improve on this, I added the option to select the type of image to be added.

A 'Gallery' image would be saved to /Assets/Images and displayed in the image gallery. An 'Advert' image would be saved to another folder and the accompanying details (ImageUrl, NavigateUrl and AltText) written to an xml file.

using
System;
using
System.Xml;
using
System.IO;

partial class WebUserControl : System.Web.UI.UserControl
{
public void Page_Load(Object sender, EventArgs e)
{
btnImage.Click += (Button_Click);
}
protected bool imageIsAd;
protected void AdRadio_CheckedChanged(object sender, EventArgs e)
{
imageIsAd =
true;
}
protected void GalleryRadio_CheckedChanged(object sender, EventArgs e)
{
imageIsAd =
false;
}

public void Button_Click(object sender, EventArgs e)
{
if (!imageUpload.HasFile) return;
if (!imageIsAd)
{

var filePath = "~/Assets/Images/" + imageUpload.FileName;
imageUpload.SaveAs(MapPath(filePath));
}

var adPath = "~/Assets/Ads/" + imageUpload.FileName;
imageUpload.SaveAs(MapPath(adPath));
WriteXml();
ImageLink.Text =
String.Empty;
AltText.Text =
String.Empty;
}

void Page_PreRender()
{
var imgFolder = MapPath("~/Assets/Images/");
var dir = new DirectoryInfo(imgFolder);
dlstImages.DataSource = dir.GetFiles();
dlstImages.DataBind();
}

Then I created the WriteXml method to perform two functions:
  1. If the xml file (Adlist.xml) does not exist, create the xml with the details of the image submitted via the form.
  2. If the xml file does exist, insert the details of the submitted image maintaining the correct nesting and formatting.
The example in the book created the file by loading in the raw xml content. I thought that it was better to create the file programmatically. One gotcha I came across was having to use the absolute path for the xml file loaded by XmlTextWriter and XmlDocument.Load.

protected void WriteXml()
{
if (!imageIsAd) return;

System.Drawing.
Image UppedImage = System.Drawing.Image.FromStream(imageUpload.PostedFile.InputStream);
float UploadedImageWidth = UppedImage.PhysicalDimension.Width;
float UploadedImageHeight = UppedImage.PhysicalDimension.Height;
var imagePath = "~/Assets/Ads/" + imageUpload.FileName;
var xmlFile = @"Absolute Path to AdList.xml";
var xmlFileInfo = new FileInfo(xmlFile);
if (!xmlFileInfo.Exists)
{
var writer = new XmlTextWriter(xmlFile, null);
writer.WriteStartDocument();
writer.WriteRaw(
"\n");
writer.WriteStartElement(
"Advertisements");
writer.WriteRaw(
"\n");
writer.WriteStartElement(
"Ad");
writer.WriteRaw(
"\n");
writer.WriteStartElement(
"", "ImageUrl", "");
writer.WriteString(imagePath);
writer.WriteEndElement();
writer.WriteRaw(
"\n");
writer.WriteStartElement(
"", "Width", "");
writer.WriteString(UploadedImageWidth.ToString());
writer.WriteEndElement();
writer.WriteRaw(
"\n");
writer.WriteStartElement(
"", "Height", "");
writer.WriteString(UploadedImageHeight.ToString());
writer.WriteEndElement();
writer.WriteRaw(
"\n");
writer.WriteStartElement(
"", "NavigateUrl", "");
writer.WriteString(ImageLink.Text);
writer.WriteEndElement();
writer.WriteRaw(
"\n");
writer.WriteStartElement(
"", "AlternateText", "");
writer.WriteString(AltText.Text);
writer.WriteEndElement();
writer.WriteRaw(
"\n");
writer.WriteEndElement();
writer.WriteRaw(
"\n");
writer.WriteEndElement();
writer.WriteRaw(
"\n");
writer.WriteEndDocument();
writer.Close();
}

else
{
var xdoc = new XmlDocument();
xdoc.Load(xmlFile);
var adverts = xdoc.SelectSingleNode("Advertisements");
XmlElement ad = xdoc.CreateElement("Ad");
XmlElement img = xdoc.CreateElement("ImageUrl");
img.InnerText = imagePath;
XmlElement width = xdoc.CreateElement("Width");
width.InnerText = UploadedImageWidth.ToString();
XmlElement height = xdoc.CreateElement("Height");
height.InnerText = UploadedImageHeight.ToString();
XmlElement navUrl = xdoc.CreateElement("NavigateUrl");
navUrl.InnerText = ImageLink.Text;
XmlElement xaltText = xdoc.CreateElement("AlternateText");
xaltText.InnerText = AltText.Text;
adverts.AppendChild(ad);
ad.AppendChild(img);
ad.AppendChild(width);
ad.AppendChild(height);
ad.AppendChild(navUrl);
ad.AppendChild(xaltText);
xdoc.Save(xmlFile);
}
}
}

Next time I would think about breaking the WriteXml method out into a class and storing image details in an database rather than in an xml file on the file system.

0 comments: