Large File Upload with C#

I wrote this a few years ago and don’t know why I haven’t posted it, so for those interested in file upload with C#. pay attention to the lMaxFileSize var. I have no doubt this can be improved upon with 4.0

Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="up.aspx.cs" Inherits="up.upload" %>

<!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>Upload Script</title>
		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
		<meta content="C#" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
	</HEAD>
	<body MS_POSITIONING="GridLayout" BGCOLOR="lightgrey">
		<form id="upForm" method="post" encType="multipart/form-data" runat="server">
			<INPUT id="File1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 160px" type="file"
				name="File1" runat="server">
			<asp:button id="cmdUpload" style="Z-INDEX: 102; LEFT: 272px; POSITION: absolute; TOP: 160px"
				runat="server" Text="Upload"></asp:button><asp:dropdownlist id="ddlPath" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 120px" runat="server"
				AutoPostBack="True"></asp:dropdownlist><asp:label id="lblMessage" style="Z-INDEX: 104; LEFT: 24px; POSITION: absolute; TOP: 200px"
				runat="server" Width="448px"></asp:label><asp:linkbutton id="LinkButton1" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 88px"
				runat="server">Reset</asp:linkbutton>
			<asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 24px; POSITION: absolute; TOP: 16px" runat="server"
				Font-Bold="True">Upload Utility</asp:Label>
			<asp:Label id="lblPathMessage" style="Z-INDEX: 107; LEFT: 24px; POSITION: absolute; TOP: 48px"
				runat="server"></asp:Label></form>
	</body>
</HTML>

Now, the details on the codebehind

Code:

'''contents of up.aspx.cs

// contents of up.aspx.cs

namespace up
{
	using System;
	using System.IO;
	using System.Web.UI;
	using System.Web.UI.HtmlControls;
	using System.Web.UI.WebControls;

	public class upload : Page
	{
		protected Button cmdUpload;
		protected DropDownList ddlPath;
		protected HtmlInputFile File1;
		protected Label Label1;
		protected Label lblMessage;
		protected Label lblPathMessage;
		protected LinkButton LinkButton1;
		private long lMaxFileSize = 10485760;
		public static string sFileDir = "";
		public DirectoryInfo[] subDirectories;

		public void BindElement()
		{
			this.ddlPath.DataSource = this.subDirectories;
			this.ddlPath.DataBind();
			this.ddlPath.Items.Insert(0, "select from list");
		}

	private void cmdUpload_Click(object sender, EventArgs e)
		{
			if ((this.File1.PostedFile != null) && (this.File1.PostedFile.ContentLength > 0))
			{
				string fileName = Path.GetFileName(this.File1.PostedFile.FileName);
				try
			{
				if (this.File1.PostedFile.ContentLength <= this.lMaxFileSize)
				{
					this.File1.PostedFile.SaveAs(sFileDir + fileName);
					this.lblMessage.Visible = true;
					this.lblMessage.Text = "File: " + sFileDir + fileName + " Uploaded Successfully";
				}
			else
				{
					this.lblMessage.Visible = true;
					this.lblMessage.Text = "File Size if Over the Limit of " + this.lMaxFileSize;
				}
			}
			catch (Exception)
				{
					this.lblMessage.Visible = true;
					this.lblMessage.Text = "An Error Occured. Please Try Again!";
					this.DeleteFile(sFileDir + fileName);
				}
			}
		}

		private void ddlPath_SelectedIndexChanged(object sender, EventArgs e)
		{
			sFileDir = sFileDir + this.ddlPath.SelectedValue.ToString() + @"\";
			DirectoryInfo info = null;
			info = new DirectoryInfo(sFileDir);
			this.lblPathMessage.Text = "Directory is: " + sFileDir;
			this.subDirectories = info.GetDirectories();
			this.BindElement();
		}

		private void DeleteFile(string strFileName)
		{
			if (strFileName.Trim().Length > 0)
			{
				FileInfo info = new FileInfo(strFileName);
				if (info.Exists)
				{
					info.Delete();
				}
			}
		}

	private void InitializeComponent()
	{
		this.cmdUpload.Click += new EventHandler(this.cmdUpload_Click);
		this.ddlPath.SelectedIndexChanged += new EventHandler(this.ddlPath_SelectedIndexChanged);
		this.LinkButton1.Click += new EventHandler(this.LinkButton1_Click);
		base.Load += new EventHandler(this.Page_Load);
	}

	private void LinkButton1_Click(object sender, EventArgs e)
	{
		this.Reset_Root();
	}

	public void myPath()
	{
		sFileDir = base.Server.MapPath("../");
	}

	protected override void OnInit(EventArgs e)
	{
		this.InitializeComponent();
		base.OnInit(e);
	}

	public void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
		this.Reset_Root();
		}
	}

	public void Reset_Root()
		{
			sFileDir = "";
			this.myPath();
			this.subDirectories = new DirectoryInfo(sFileDir).GetDirectories();
			this.BindElement();
		}
	}
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

This week, I got my hands into IIS7 - yes, I know I am running behind a tad. Since I manage more than I code and administrate these days, it’s a wonder I do any of this. So, I ran into a very simple problem. How do I start IIS7? I don’t want all the pages of explanation and messing about. Just tell me where I turn the key? You think this would be an easy question to answer but I guess not. First, it looks like it is installed - it ain’t. To prove it - open the command prompt (start > run > type cmd); type the following: iisreset
If this gave any kind of an error, chances are you need to add a role to install.

So, without all the geek-speak, here’s how to Start IIS7 for Windows Server 2008 after adding the correct role. BTW, after doing this, it really is a good idea to familiarize yourself here: http://www.iis.net/

  • Right click on Computer and choose Manage or Start - Admin tools - Computer Management
  • Expand Roles under Computer Management
  • Right click on Roles and choose Add a Role
  • Choose Server Roles on the left
  • Choose Web Server (IIS) and follow through to find what ya need

Now, you can stop and start with ease.

  • Look on the left pane under Roles, highlight Web Server (IIS)
  • Now look to the middle bottom pane and highlight IIS Admin Service
  • Finally to the far right, you see Stop, Start, Restart etc.

Or, you could open the command prompt and type iisreset of course. Cheers!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

nVidia GeForce and Vaio Black Screen

OK, I  just had awful trouble getting my Sony Vaio to accept the new nVidia GeForce 6_5 driver.  I searched the internet and found little relief, but I have managed to fix this little bugaboo.  I figured as many help posts as I’ve seen; I’d provide my individual solution.  I don’t know that this will work for everyone, so set a restore point before doing any work.  Also, keep in mind your safe mode if you need to uninstall the driver as I needed to do 6 times in my attempts to get everything working.

My scenario: The kid was playing some online game on my developer station.  I have dual displays set up for this machine.  Everything was fine and I’d obviously made this work a year ago.  To make a long story short, big bad bug attacked my System32 directory and killed the machine for all intense purposes.  It was caught in an endless loop of reboots.  I removed the hard drive and placed it into my Belkin USB enabled enclosure and copied my files to another machine easily.  Next, I placed back in the Vaio and proceeded to reformat and reinstall Windows XP Pro.  No issues with this.  Remember, the nVidia (BFG) card has been in the machine the entire time.  Now, I have rebuilt, I am ready to begin reinstalling drivers and customizing.  I skip past the registration and just get to work.  That may be the problem…

I get most drivers going, except for the nVidia.  What’s it look like?  I install with the disk, but the Vaoi has no obvious way to disable the onboard video.  This is a supposed requirement.  I enter the BIOS with F2, to find there isn’t an obvious way to disable here either.  I try and try to play with the settings, choosing PCI/AGP to no avail.  When I install, and reboot I get a black screen.  I am able to enter Safe mode to uninstall so I can see what I am doing.  Now the refresh rate is crappy again.

In the end, I choose to begin updating the rest of the system.  I verify my copy of Windows and begin updating.  First, I went ahead and installed the driver (without the disk this time) and chose not to restart.  I then updated windows to SP2 and continued.  I then rebooted the system and lo and behold, after choosing my security settings I was able to view and configure the card for dual screens and everything is working as it should.

I don’t know if this will work for everyone, but it may be worth a try.  Let me know if this helps anyone else.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

Recent work

Here are a couple of recent jobs.  If you like what you see, contact me.  Current pricing is reduced.  Sean.Ryan@hireageek.net
Neyland Designs:


The Unger Law Firm:

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

msxml3.dll error ‘80070005′

This little error is causing some trouble for developers, particularly those in charge of maintaining legacy code in VB and vbscript. The trouble has stemmed from a recent microsoft service pack: Microsoft Security Bulletin MS07-042 - Critical (Vulnerability in Microsoft XML Core Services Could Allow Remote Code Execution (936227))

I have seen this everywhere on my travels across Google. People are trying all kinds of things to fix the error in the title. It may also manifest itself as msxml2.dll error ‘80070005′ Access Denied. There was a critical patch needed to secure things further, so it’s a good thing to take care of. That doesn’t take care of the headache.

So, to the code.

Should you have something similar to:

Set xml = Server.CreateObject("Microsoft.XMLHTTP")

Replace it with something like:

Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")

I hope this helps.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

Batch files aren’t dead yet

OK, I can hear the Powershell, WSH, Ruby, Perl and other such fans geeking out already, but the truth is the command line shell DOS isn’t a dead horse yet. It still provides some very friendly ways of performing automated tasks. I have put together a group of bat files that perform weblog backups. You could use these files to perform other kinds of backups if needed. Also for those out there who still like to play in DOS, there are a few neat little tidbits within the group of files. I don’t usually place code in my entries, but this worked well, and I figured out much of it by perusing the web and getting some bits from here and there to perfect the toolset. So, thanks everyone.

Now, to use these bat files you will need two utilities. The first is a little add-on for the command line called doff which makes formatting dates very easy. You can find this at http://www.jfitz.com/dos/. Just copy this file into Windows\System32. You don’t need to run a regsvr on this either. Next, because it is rather commonplace, I chose to use winzip for the zipping portion of the action set. You could easily change this to pkzip or some other compression utility that has a command line option.

OK, if you have all that set up - you can begin. The next piece I will share is that you’ll need to check your logfile nomenclature. In this example I am pulling hourly logs based on the previous day. So, I download logs that look like ex07082300.log, ex07082301 and so on. The common element is the 23 in this case. So, today is the 24th and I want to download yesterdays logs into a date created directory with the name of the day as the name of the zip archive. Make sense?. There are four files in this example, because I like to separate functionality in my code rather than putting it all in one.

Once you copy these files you can run it on a schedule. To do this you can run schtasks in XP and higher (start > run > type cmd > type schtasks /?) or AT in previous versions (type AT /?). The file you want to schedule is localBackup.bat.

Click here or right click-save link as… to get the zipped folder with all the contents. There are six files in the toolset, but the two script files are recreated on the fly from a called bat file each time it is run. They are there to share the example. To conclude, please test this and let me know if there are any date based bugs. I haven’t run this on the first of the month or last day of a month yet. As with all code from the web, please use at your own risk and feel free to modify and share as you like.

Update: The files worked on the first day of the month.  It downloaded the last day of the previous month as needed.  This has yet to break on me, so there it is…

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

Broken link management

Recently I was assigned a task to run through a specific dataset and determine all broken or outdated links within a database. I didn’t really want to write a specific 404 script so I improvised. I created a web form not long ago that would produce an asp page that pulls data from a variable entered data source and driver. Based on entry it will include a specific driver and a dataset location. In this instance I was working with a Foxpro source. In a matter of seconds I had a page that would dump all links to a single page. This also helps my reporting. Next, I jumped out to download.com to see what I could find to help me read the page I had generated. I found a nice little tool called Xenu http://home.snafu.de/tilman/xenulink.html. I downloaded and installed the tool and ran the tool against the new page locally.

The tool ran through the page and showcased all links that were broken; either not found, redirected, no such host, etc. Cool! Next, Xenu allowed me to export the data to a delimited file, and I imported that into a new Fox table I could query against. I pulled together a decent report and handed this off to an admin cleanup crew. Out of over 1,400 links, 250 or so showed results other than success.

The next step is to figure out how to take this new dataset and run a compare. Within this I want a condition that states, if the link is broken, then send to archive table and delete from current.

Of course, the best way to perform this kind of work is to automate the entire process, but in all – this took about 30 minutes. It was enough for what I needed to get done right away. If I have time to get to automation and make it work I will share the method. If anyone else has such an automated link management methodology for databases, feel free to share.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

Cool site

I found this site today and thought it was worth sharing. It may not be technology, but it serves plenty of geek purpose. I think it is worth a look. timelineindex. It has some interesting tidbits and of course you can search wikipedia for additional info on some of the timelines.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

Slickrun cleans thy desktop

Slickrun cmd windowA mate of mine, netguru - also available under the hireageek umbrella at netguru.hireageek.net showcased a few tools he found fun and interesting for me last week. I found one of those tools, well… outstanding. It is a little lightweight application called Slickrun. Search for it at download.com. Basically, it places a little transparent run command window on your desktop region. By default, it places the little cmd win on the bottom right of the screen. Big whoop; right? Yes, in fact! I have rid my desktop of all superfluous items and simply have this little gem now.

Here’s what it does. Say, you love Firefox (Yes, yes I do). Open up the magic words on Slickrun and drag that icon sitting on the desktop into the slickrun app, give it a memorable name (I chose “fire”), and viola; from now on, when launching Firefox, use Windows-Q(default - you can change this too) to set the cursor into the cmd area, and type “f”. It will try to fill in the rest for you, AJAX like; hit enter and there you go.

You can choose lots of programs to do this with. Here are some I have setup in the magic words menu “Word” for MS Word, “mail” for Outlook Mail, “localhost” for my development window. “dNET” for Visual Studio.NET… and on and on.

Simply put, I really love this little application.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon

Experimenting with Themes

I will spend a little time setting up some themes for the new design of the site.  For now, I am going to look around at some of the excellent templates available online and through the wordpress console.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • Slashdot
  • Furl
  • StumbleUpon