Friday, March 6, 2009

Streaming a zip file from an ASP.NET web site

I was recently having a really tough time trying to debug the streaming of a zip file from an ASP.NET web app to a browser. The code was fairly simple and is repeated all over the web which gave me confidence in how robust it is:

FileInfo toDownload = new FileInfo(fullFileName);
if (toDownload.Exists)
{
   Response.Clear();
   Response.ContentType = "application/zip";
   Response.AppendHeader("Content-Disposition", "attachment;filename=" +
          toDownload.Name);
   Response.AppendHeader("Content-Length", toDownload.Length.ToString());
   Response.TransmitFile(fullFileName);
   Response.End();
}

The client side error that I was getting was:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near 'PK...'.

The good thing about seeing the "PK" in the error is that it told me that the server was indeed sending the zip file. The first two characters in a zip file are PK which is one way to identify a zip file by opening it and reading the first two characters.

The control on the web page that kicked off the download action shown above was inside an UpdatePanel and it was this that was causing the problem. Eilon Lipton wrote the error message above and he explains it in this blog post. 

The solution it turns out is to put a PostBackTrigger in the UpdatePanel:

<Triggers>
   <asp:PostBackTrigger ControlID="ibDownload" />
</Triggers>

This will force the click to do a regular postback instead of an asyncronous postback and solved the problem.

Wednesday, March 4, 2009

Skewed results

Most web sites collect stats on usage. One of these figures is browser type and version.

I've just been using H&R Block for my tax returns this year and find that their site won't work well with anything other than Internet Explorer (IE). Whenever I get an email from them saying that there's a message I fire up IE to go and check the message.

Now the problem with this is that their stats will show that most of their users are using IE. This is because anybody using them will be forced to use IE. So they will never know which browser their users usually use and thereby creating skewed results for their site usage.

Monday, February 16, 2009

Adding a Canonical Link Element in ASP.NET

Google, Microsoft and Yahoo announced support for a new "canonical" link element in the header of a page that will help clean-up duplicate url's that have been indexed from your site.

The canonical link for this page, for example, would be:

<link rel="canonical" href="http://guyellisrocks.com/coding/adding-a-canonical-link-element-in-asp-net/">

To add this link tag to your header tag programatically using C# add the following to your Page_Load() function:

        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlLink link = new HtmlLink();
            link.Attributes.Add("rel", "canonical");
            link.Attributes.Add("href", "http://guyellisrocks.com/coding/adding-a-canonical-link-element-in-asp-net/");
            this.Header.Controls.Add(link);
        }

 

Friday, February 6, 2009

Why Silverlight is the Future

Rockford Lhotka wrote a good post post yesterday Why Silverlight is the Future.

I'm a Silverlight fan and agree with him. The sheer volume of .NET developers alone make Silverlight a highly likely candidate for success.

One thing that he didn't mention and that I think will push Silverlight to mass acceptance is that it will, at some point in the future, be hostable on mobile devices. Silverlight is just a control (or collection of controls) on a web page. The browser, and the Silverilght runtime/VM host the control. So mobile devices only need a host for Silverlight and then Silverlight will become ubiquitous.

Will Moonlight 2.0 be that host for most mobile devices?

Neil McAllister wrote yesterday The case for supporting and using Mono in which he says:

There's no need to code in C++ for Windows and Objective-C for the Mac -- the same project can be built for either platform from the same C# and JavaScript sources.

And that's the crux of it.

Wednesday, February 4, 2009

Training a text classifier

When writing a text classification system you need to train it. Typically you have a corpus of good data that has been accurately pre-classified and this is what you throw at the system while it is learning the classification.

I came up with what I thought was a good analogy for an untrained text classifier: A genius amnesic. i.e. someone who initially knows nothing but learns lightening fast.

Ultimate Dogfooding

Visual Studio 2010 (VS2010) is being re-written using Windows Presentation Foundation (WPF). I was just listening to Scott Hanselman's 165th podcast in which he interviews Noah Richards about the editor in VS2010. What impresses me is that they're building VS2010 using VS2010. I can only assume that the first couple of builds of VS2010 were done with VS2008 and then I guess that once they had a working version they switched.

This must be the ultimate dogfooding exercise. Building a product using the product.

Tuesday, February 3, 2009

Text Classification References

I'm currently working on a text classification system. This is requiring a fair amount of research and background reading so I'm going to create a list of references that I'm using:

A Plan for Spam by Paul Graham

Better Bayesian Filtering by Paul Graham

Bayesian Filtering: Beyond Binary Classification [PDF] by Ben Kamens

Ending Spam: Bayesian Content Filtering and the Art of Statistical Language Classification by Jonathan Zdziarski

CRM114 Discriminator by Bill Yerazunis

A free online Bayesian Classification service that I recently found and tried is called uClassify. I found that it was remarkably accurate and contacted the owner and exchanged some emails with him. Unfortunately he uses a proprietary data store that he bundles as part of the commercial package that he sells which makes his product unscalable and impossible to fail-over. Hopefully one day he'll move the datastore so something like SQL Server to make this product more usable by more people.