Friday, January 23, 2004

Force download file (ASP)

Any file on a web server can be downloaded to the client. The IUSR_MACHINENAME (or Everyone) account needs read access to that file for it be be downloaded. MDAC 2.5 or later is needed for this to work - http://www.microsoft.com/data/download_250rtm.htm

JScript:

// clear buffer
Response.Buffer = true;
Response.Clear();
// path to file
var sFilePath = Server.MapPath("/path/to/file.pdf");
// attachment name (without extension), this will be what the name of the downloaded file will be
var sAttachName = "attachmentname";
// remove spaces, else file may not download
sAttachName = sAttachName.replace(/\s/g,"");
// create filesystem object
var oFSO = Server.CreateObject("Scripting.FileSystemObject");
// check if file exists
if(!oFSO.FileExists(sFilePath)){
	Response.Write('<strong>File does not exist. <a href="'+Request.ServerVariables("HTTP_REFERER")+'">Go back</s>.</strong>');
	Response.End();
}
// get file
var oFile = oFSO.GetFile(sFilePath);
// get file size
var nFileSize = oFile.Size;
// get extension
var sExt = sFilePath.substring(sFilePath.lastIndexOf("."));
// download as attachment
Response.AddHeader("Content-Disposition","attachment;filename="+sAttachName+sExt);
// add content length header (for showing progress of download)
Response.AddHeader("Content-Length", nFileSize); 
// set to always download by setting content type
Response.ContentType = "application/octet-stream";
// create stream
var oStream = Server.CreateObject("ADODB.Stream");
// open stream
oStream.Open;
// set type of stream to binary
oStream.Type = 1;
// load data from file into stream
oStream.LoadFromFile(sFilePath);
// send data to client
Response.BinaryWrite(oStream.Read);
// close stream and end output to client
oStream.Close;
Response.End();

VBScript:

Option Explicit
' declare variables
Dim sFilePath, sAttachName, oFSO, oFile, nFileSize, sExt, oStream
' clear buffer
Response.Buffer = True
Response.Clear
' path to file
sFilePath = Server.MapPath("/path/to/file.pdf")
' attachment name (without extension), this will be what the name of the downloaded file will be
sAttachName = "attachmentname"
' remove spaces, else file may not download
sAttachName = Replace(sAttachName," ","")
' create filesystem object
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
' check if file exists
If oFSO.FileExists(sFilePath) = False Then
	Response.Write("<strong>File does not exist. <a href="""&Request.ServerVariables("HTTP_REFERER")&""">Go back</s>.</strong>")
	Response.End
End If
' get file
Set oFile = oFSO.GetFile(sFilePath)
' get file size
nFileSize = oFile.Size
' clear file system object
Set oFile = Nothing
Set oFSO = Nothing
' get extension
sExt = Right(sFilePath, (Len(sFilePath) - InStrRev(sFilePath,".",-1,1) + 1) )
' download as attachment
Response.AddHeader "Content-Disposition","attachment;filename="&sAttachName&sExt
' add content length header (for showing progress of download)
Response.AddHeader "Content-Length", nFileSize
' set to always download by setting content type
Response.ContentType = "application/octet-stream"
' create stream
Set oStream = Server.CreateObject("ADODB.Stream")
' open stream
oStream.Open
' set type of stream to binary
oStream.Type = 1
' load data from file into stream
oStream.LoadFromFile sFilePath
' send data to client
Response.BinaryWrite oStream.Read
' close stream and end output to client
oStream.Close
Set oStream = Nothing
Response.End

Monday, January 19, 2004

Convert Date to ISO (SQL Server)

To convert a date column into ISO format (YYYY-MM-DD) using SQL, the following code can be used:

LEFT(CONVERT(char(10), DateColumn, 126), 10) AS DateFormatted

Or using '/' as the seperator:

REPLACE(LEFT(CONVERT(char(10), DateColumn, 126), 10), '-', '/') AS DateFormatted

Friday, January 16, 2004

Sizer - Change size of windows

Very useful freeware for resizing windows (for Microsoft Windows based PC's). Good for testing webpages at different resolutions without changing the screen resolution.

Download from: http://www.brianapps.net/sizer.html (73kb exe installer, 17kb zip file)

Friday, January 09, 2004

Open Centered Popup Window (JavaScript)

The following script will open a centered popup window, maximum size 800 x 600. If the user has a resolution lower than 800 x 600, it will fill the screen.

function openWin(page){
     // width
     w = (screen.width>800) ? 800 : screen.width;
     // height
     h = (screen.height>600) ? 600 : screen.height;
     // left (centered)
     l = (screen.width-w)/2;
     // top (centered)
     t = (screen.height-h)/2;
     // open new window
     newWin = window.open(page,'newWin','left='+l+',top='+t+',width='+w+',height='+h);
}

Wednesday, January 07, 2004

Manipulate URL QueryString (ASP JScript)

The following functions modify the QueryString in a URL by removing and adding parameters.
// sURL = URL to append to, sParam = parameter name, sParamVal = parameter value
function appendToURL(sURL,sParam,sParamVal){
	// remove existing parameters
	sURL = removeFromURL(sURL,sParam)
	// remove ? if it is the last character on the URL
	sURL = sURL.replace(/\?$/,'')
	// generate the querystring (add '?' if querystring is blank, '&' if there are other parameters)
	sURL += ((sURL.indexOf("?") == -1)?"?":"&") + sParam + "=" + sParamVal;
	return sURL;
}
// sURL = URL to remove from, sParam = parameter to remove from URL
function removeFromURL(sURL,sParam) {
	// where the querystring starts (i.e. where ? appears)
	var qStart = sURL.indexOf('?') + 1;
	// if ? is not in sURL (i.e. qStart = 0) or ? is at the end of sURL, then there are no parameters to remove
	if (qStart == 0 || qStart == sURL.length-1) return sURL;
	/* create regular expression, match sParam followed by '=', then any character (except '&') 1 or more times
	and optionally ending with a single '&' (optional as there may be a parameter after this one, which will need to be preserved) */
	var regexp = new RegExp("(" + sParam + "=[^&]+(&)?)");
	/* return the URL, first by getting the root url (which ends at qStart),
	and then replacing any text that matches the regular expression, as well as making sure the url does not end with '&'
	*/
	return sURL.substring(0,qStart) + sURL.substring(qStart).replace(regexp,'').replace(/&$/,'');
}