January 2007 - Posts

HttpResponse.WriteFile and the file name.

The problem, your user uploads a file, 'aFile.txt', to your web site, the web site processes it and generates another file, which you temporary save under the filename 'tmp789.tmp'. You then send that temporary file to your user as a download, but you don't want it to be named tmp789.tmp. So how do you change the download name of the file being sent using HttpResponse.WriteFile()?

Here's how....


HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + myNewFileName));
HttpContext.Current.Response.ContentType = "application/xml";
HttpContext.Current.Response.WriteFile(pathToFileBeingDownload);
HttpContext.Current.Response.End();

Be sure and set the ContentType and also make sure you call the HttpResponse.End() method!

Posted by dsmyth
Filed under:

ASP.NET AJAX v1.0

I'm sure your all aware, but if not, Microsoft have released v1.0 of the ASP.NET AJAX, previously know as Atlas. They had planned to release it as part of Orcas but it was deemed to groovy.

On a related subject I tried out the I.E. Developers Toolbar Beta today, it was a quick look, and although it's still Beta it's worth the download.

Posted by dsmyth

The StopWatch class

Today I came across a new class in the .NET v2.0 that I've never seen documented before; the System.Diagnostics.StopWatch class. No introduction necessary.

A Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.

A Stopwatch instance is either running or stopped; use IsRunning to determine the current state of a Stopwatch. Use Start to begin measuring elapsed time; use Stop to stop measuring elapsed time. Query the elapsed time value through the properties Elapsed, ElapsedMilliseconds, or ElapsedTicks. You can query the elapsed time properties while the instance is running or stopped. The elapsed time properties steadily increase while the Stopwatch is running; they remain constant when the instance is stopped.

Groovy.

Posted by dsmyth
Filed under:

Generic Collection Library

At last I have something worth blogging. I'm currently studying for the MCPD upgrade exams and word on the street is generics will be a large part of the first exam. Just so happens I've got a nice little book on the subject that's just the ticket. The books called Professional .NET v2.0 Generics, it's published by Wrox, and it's a pretty good book with plenty of easy to understand information.

The book covers all parts of generics but it also goes into detail about the generic classes that are availablein the free collection of .NET classes called Power Collections developed by developers, but compile and hosted by the good people at Wintellect. I've yet to explore it in any detail but think it was worth a blog.

Posted by dsmyth