Tip: File / Open / URL - Jon Galloway

Tip: File / Open / URL

As web developers, we’re constantly having to work with files that are provided via URL. The simplest case is a web page source HTML, but there’s also XML config files, images, documents (PDF, PSD, DOCX, etc.). Usually we don’t think about it, because they’re linked in a web page, so we we do the right-click/save file, then open our editor and do the file/open/browse… where did I save it again… dance. If we remember to shift-right-click/copy as path trick, we think we’ve cheated the system just a bit.

It’s even worse if we just have a URL that’s not in a webpage. How many times have you fired up notepad to type a one-line HTML file with something like this: <a href=”http://domain.com/file-i-want.xml”>click me</a>? Then you open that file in a browser, right-click, save file, dum-de-dum…

There’s a better way!  (Yes, better than wget)

Last year I was working on a video site that streamed ASX files through a pretty complex Silverlight video player. ASX is one of those funny file formats that looks a lot like XML, but isn’t, and to top it off there are some funky ASX features that Silverlight doesn’t support. So I ended up having to write an ASX parser, and doing a lot of testing against our video providers every time something strange happened with our various video feeds. I was constantly copying URL’s into one-off HTML files, and eventually I just had to go looking for a better way.

It turns out that you can open URL’s directly in Visual Studio. Try it!

Open Visual Studio (any version) and hit Ctrl-O to bring up an open file dialog, then type in a URL. Here I’m using http://microsoft.com:

File-Open-Microsoft-URL

When I click enter, the HTML is requested and displayed:

File-Open-Microsoft-HTML

The Fun Don’t Stop – It Works With Other Programs And Filetypes, too!

Let’s fire up good old MSPAINT and open the .NET Logo from this URL: http://www.microsoft.com/presspass/images/gallery/logos/web/ms_net_rgb_web.jpg

File-Open-MSPAINT

Now let’s try something crazy – let’s open that same URL in Inkscape, a cross platform vector graphics editor:

File-Open-Inkscape

What’s going on?

It turns out that the Windows Open File Dialog supports URL’s, so as long as the app isn’t using a custom dialog it just magically works. The files are actually downloaded to your Temporary Internet Files folder and opened from there. Aside from the speed of opening the files directly, there’s another benefit: rather than scattering temporary files all over your documents folder or desktop, they’re in the right place to be automatically cleaned up when you clear your temporary files.

What about saving a file, rather than opening it?

Well, it’s pushing it a bit, but for simple cases, you don’t really need an app or a dummy HTML file, either. If you’ve got a file that IE won’t natively open, you can just hit Start and type: iexplore “http://domain.com/importantfiles.zip”

At that point, though, you might as well just fire up Powershell (built into Windows 7, hit Start / type Powershell / enter) and run a one-liner:

(new-object System.Net.WebClient).DownloadFile(“http://domain.com/importantfiles.zip, "localfilename.zip")

There are some other tips about downloading from the commandline on this question on SuperUser.

Why not wget / wfetch / “Crazy Eddie’s Download Powerpack” / etc.?

It’s a fair question.

Those all work, and they’re great if you’re used to them. I’ve really been trying to simplify lately, though. One of the benefits of Windows 7 is that it doesn’t need a bunch of random shareware tweaks to work well, it just works. Could I install a file downloading app? Sure, but when Windows has so much built in, there’s no real need for most simple tasks. The big benefit is that I can hop on any Windows machine and do my thing, without having to download a bunch of extra unitaskers.

Published Thursday, January 7, 2010 12:04 AM by Jon Galloway
Filed under:

Comments

# re: Tip: File / Open / URL

I think there's some merit in being able to type 'wget <url>' rather than '(new-object System.Net.WebClient).DownloadFile(<url>)'

Thursday, January 7, 2010 12:32 PM by nATHAN

# re: Tip: File / Open / URL

@nATHAN: function wget($url, $dest) { (new-object System.Net.WebClient).DownloadFile($url, $dest) }

Thursday, January 7, 2010 1:39 PM by jdhardy

# re: Tip: File / Open / URL

I've always loved this with Textpad. Type in a URL into the open dialog and get the source for the page.

Friday, March 19, 2010 3:19 PM by RyanFarley

# re: Tip: File / Open / URL

Awesome! Thanks :)

Pete

Thursday, March 25, 2010 2:33 AM by Pete