Send file data via JSON from REST API and open as download file in browser (with Classic ASP/JQuery)

Pet project challenge of the day. I have a REST API that delivers a JSON response, and I want it to include file data, so the browser at the client end can download the file. In real world what I’m doing is offering a link to the user to export some data to an XLS and download to their desktop.

I messed around with this for a while. Carriage of the file data seemed simple – at the REST end encode it to base64 and stick that into the JSON. The problem lies at the client end. I have JQuery calling the REST, and dealing with the response. How then to get the base64 out of the JSON and contrive to offer it as a File Save As situation?

Finally here’s what I came up with, it was sparked by something I saw on StackOverflow, although I cannot lay my hands on the post right now:

At the REST, encode the XLS file text to base64, and return the base64 in the JSON as a text element

On the client, I have an ASP script that acts as the controller, it receives requests from the pages (eg JQuery calls this page), then it calls the remote REST, and returns the response back to JQuery. Obviously cross-browser rules means my JQuery can only call URLs on the same domain, and my REST is not. So this script acts as the transporter for the calls. It’s even called Frank.

On the page I have a form with a hidden field, JQuery puts the base64 file data into that hidden field, then submits the form to another page.

<form id="base64Form" action="openFileToBrowser.asp" method="post" name="base64Form">
<input id="base64data" type="hidden" name="base64data" value="" />
<input id="fileName" type="hidden" name="fileName" value="responses.xls" />

This new page grabs the file data, decodes it from base64 back to text, sets the content headers to XLS etc. And hey presto, the user gets a File Save As dialog. Here’s the code on that page:

<% Option Explicit%>
<% response.expires = 0 %>
<!--#include virtual="/includes/hex_sha1_base64.asp" -->
	dim base64data
	if base64data<>"" then
	Response.ContentType = "application/"
	Response.AddHeader "Content-Disposition", "attachment; filename="&request.form("fileName")
	response.write Base64_Decode(base64data)
	end if

The base64 encode/decode routine I’m using is part of the very handy hex_sha1_base64.asp file which I also use for other encryption and encoding tasks (for example I use it for encryption as part of talking to OAUTH based APIs).

I haven’t tested this in all browsers yet, be interesting to see if kicks up problems with IE etc.

nvarchar(max) doesn’t work on Azure SQL with Classic ASP


This is one of those completely obtuse issues that one hits from time to time – nvarchar(max) fields behave oddly on Microsoft Azure SQL – yet it all works fine on SQL Server 2008 on my local machine.

I battled this for an hour before finally simultaneously figuring out that trying to return data from an nvarchar(max) column via Classic ASP on Azure yields a blank value (or weird binary garbage); and falling over someone else who’s had exactly the same problem recently.

It’s fine to have a table with nvarchar(max) in Azure. I can push data into it, I can see the data via SSMS, but returning via ASP – nada.

I’m returning the data via a UDF, so I altered the table definition returned from the function so the column is nvarchar(4000). My function grabs the data from the (max) column and sticks it into the output column without trouble, and the function returns values fine, don’t even need a CAST.

It’s frustrating because I’m anticipating these columns to hold large blocks of text, and now I’m size restricted.

I haven’t had time to explore whether messing with the connection strings might do the trick.

Clean up double line spacing in Dreamweaver code


I work across Mac and Windows, with Dreamweaver running on my Mac, and code that sometimes will be hosted on Windows Servers and sometimes not. So I often run into the problem of erratic line spacing. If I have code saved on my Mac HDD (as opposed to my Windows VM), push it up to a Windows Server, then pull it down again, I sometimes wind up with the code double spaced when I open the file again in Dreamweaver.

I finally tracked down a quick solution to clean up these pages, with grateful thanks to this page: Permanently Remove Double Space in Dreamweaver Code View.

Do a search and replace:

  • Search for: [\r\n]{2,}
  • Replace with: \n

Make sure you have the Use Regular Expressions option checked.

Of course, it’s also good to make sure you have the Line Beak Type set correctly under Dreamweaver’s Preferences, but I’ve found that doesn’t really help when you are working cross-platform.