Monday, February 1, 2010

Where's My File? Server.MapPath to the Rescue!

During the task of trying to serve up dynamic image content from a database, it turned out that in certain circumstances I also needed to read a local image file on the server too.

This was for the case in which no data was found or was missing for some reason -- basically an "Image Not Found" image. The problem is that I had not had to deal with the problem of locating a file in the web application file hierarchy before, strangely enough.

So when I tried to use the File.OpenRead() method, each of the various values for the path I tried didn't work properly and the file could not be found.

Thankfully, I found this post on the ASP.NET forums:

How to read image file ? - ASP.NET Forums

The important piece of information for me was the bit about using Server.MapPath() -- that was the crucial piece of the puzzle I was missing.

So here is what I came up with:

protected byte[] GetImageFromFile(string fileName)
{
byte[] image = new byte[1];
image[0] = 0;

try
{
string filePath =
Server.MapPath("~/images/" + fileName);

if (File.Exists(filePath))
{
FileStream fs = File.OpenRead(filePath);
image = new byte[fs.Length];
fs.Read(image, 0, image.Length);
}

}
catch (Exception ex)
{
logger.Error(ex);
}

return image;
}


Then I call this method and write it out with Response.BinaryWrite() (shouts out to Derek Pinkerton as well for his good blog post on the subject of Response.BinaryWrite() - it was very helpful in this process as well...)

I could genericize it to retrieve a file from anywhere, for now I YAGNI'ed it since I really was just needing to serve up image files from the image directory. If that changes in the future I can very easily modify it or create a more general method for handling that. A trivial modification.

Tuesday, November 24, 2009

Page_Load event firing twice

This has not been a really great week for me and ASP.NET...

I ran into this problem after fixing the other problem:

Page_Load event is firing twice - ExtremeExperts

So with that information, coupled with this information:

Stackoverflow.com - ASP.NET Page.OnLoad executes twice

...and this item from the Microsoft KB:

How to use the AutoEventWireup attribute in an ASP.NET Web Form by using Visual C# .NET

...I was able to solve this double-page-load problem since I was, in fact, wiring up events in a page that had AutoEventWireup set to "true" and the Page_Load was getting fired twice. Awesome.

Anyway, another bullet dodged.

Dynamically Created LinkButtons and UpdatePanel Woes

Check out my pain as described in this ASP.NET forum post:

Button command not working inside an UpdatePanel - ASP.NET Forums

Totally ridiculous how much work I had to do to get this function working.

Wednesday, November 18, 2009

ASP FileUpload Controller in AJAX Accordion - Stack Overflow

When working with ASP.NET, then ASP.NET AJAX, invariably you end up nesting all sorts of wacky controls inside one another.

I had been carefully avoiding putting a FileUploader inside an UpdatePanel since generally it means headaches because the FileUploader security model does not really fit into the AJAX model... maybe someday it will.

At any rate, I ended up with an FileUploader inside an Accordion inside an UpdatePanel, and of course, it stopped working since it needed a full postback, not an AJAX partial postback.

So this post on Stackoverflow.com is probably the most succinct solution to the problem I've seen and it totally makes sense:

ASP FileUpload Controller in AJAX Accordion - Stack Overflow

Here's the code:


<ajaxToolkit:AccordionPane >
<Header><asp:LinkButton ID="lbtnOption1" runat="server">Option 1</asp:LinkButton></Header>
<Content>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
/* Put HtmlInputFile and upload button here*/
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
</Triggers></asp:UpdatePanel>
</Content>
</ajaxToolkit:AccordionPane>

Monday, October 5, 2009

Slumming it with IUI and ASP.NET

With all the hype surrounding the iPhone, the SDK, and building applications for the App Store (which recently tipped the scale at 2 billion downloads), I'm taking a slightly different angle and am exploring "old-school" iPhone develompent: Web Development.

I was just mulling things over and it seems to me that there still is definitely a market (or rather, a need) for iPhone web applications.

So I'm investigating IUI, since it's relatively "mature" but the thing I'm concerned about is how well it integrates (or doesn't) with ASP.NET. So that's what I'm going to be exploring in the near future.

Friday, August 14, 2009

Big Shout Out for Panic's Coda - Awesome Web Development Tool for Mac OS X

Over the last few days I've discovered Coda from Panic:

Panic - Coda - One-Window Web Development for Mac OS X

It is a truly awesome tool for doing web development and it does exactly what I want it to:

  • By default it opens to the file system on the left pane, with the editor on the right

  • It just works.

  • It's code sensing (a la IntelliSense) is fun!

Anyway it's definitetly worth checking out.

Wednesday, June 17, 2009

Notepad++: How to Associate Syntax Coloring for a File Extension

For the life of me, I could not figure out how to permanently associate the XML syntax coloring for files with a .config extension in Notepad++.

The help did not help, nor did the FAQs.

At any rate, this is how you do it:
  1. Settings Menu --> "Styler Configurator" (which then displays the "Style Configurator" window (note the discrepancy)).
  2. Click on "XML" in the Language list on the far left.
  3. Notice at the bottom there are two text boxes: Default ext and User ext.
  4. Enter config in the User ext text box.
  5. Click Save & Close.
  6. Open a file with .config extension - file should be automatically highlighted.
Here's a suggestion for Notepad++ developers (since it doesn't seem as if they have UX or Interaction Designers on the team)...

Help the user out by doing one of the following:
  • When an unrecognized file is opened ask what kind of file it is, or
  • When the user clicks on an entry in the Language menu, save it as the default for files with that extension.
Thanks!