ASP.NET : Download Manager Control

If you find yourself bashing your head trying to get files to download properly, or you would like more security with your downloads, try creating a download manager control. It is fast and easy.

Inside your page, we’ll make our hyperlink.

To download the file, <b><asp:HyperLink ID="uxhyperlink1" runat="server" Text="click here" NavigateUrl="#" /></b>.

Inside the page’s code behind, we’ll add the proper URL.

 String filesPath = "../Files/"; //perhaps yours is different
 String fileName = "instructions.pdf"; //perhaps yours is different
 uxhyperlink1.NavigateUrl = "DLhandler.ashx?fname=" + filesPath + fileName;

Finally, we’ll create the ASHX handler, DLhandler.ashx :

<%@ WebHandler Language="C#" Class="DLhandler" %>

using System;
using System.Web;

public class DLhandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        String fileName = context.Request.QueryString["fname"];

        if (fileName != null && fileName.Length > 0)
        {
            //if there is a file...
            var r = context.Response;
            r.Clear();
            r.Buffer = true;
            r.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
            r.ContentType= "binary/octet-stream";
            r.WriteFile(context.Server.MapPath(fileName));
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}
Advertisements

SQL Transaction

USE [MyDatabase]
GO
/****** Object:  StoredProcedure [dbo].[ExampleTransaction]    Script Date: 08/18/2009 10:02:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		me
-- Create date:   08/16/2009 10:02:31 PST
-- Description:	...
--		 		...
-- =============================================
ALTER PROCEDURE [dbo].[ExampleTransaction]
--(
-- -- ...
--)
AS
	/* SET NOCOUNT ON */

	DECLARE @pkeyID INT
	DECLARE @error INT
	SET @pkeyID = 0
	SET @error = 0

	BEGIN TRANSACTION 
	
		INSERT INTO MyTable (...) VALUES (...)

	SET @error=@@ERROR
	IF @error <> 0
	BEGIN
		ROLLBACK TRANSACTION
		SET @pkeyID = -1
	END
	ELSE
	BEGIN
		COMMIT TRANSACTION
		SET @pkeyID=@@IDENTITY

	END

	RETURN @pkeyID

Javascript Utility Functions

Change Class Names

Example: changeClassNames( "metercontainer", "metercontainer_hidden");

function changeClassNames(oldClassName,newClassName) { //thanks, google
var divs = document.getElementsByTagName(‘div’)
for (var i=0;iGet Elements By Class Name

Example: var elementsArray = getElementsByClassName(document, 'div', 'meterbar');

function getElementsByClassName(oElm, strTagName, strClassName){ //thanks, google
var arrElements = (strTagName == “*” && document.all)? document.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, “\\-“);
var oRegExp = new RegExp(“(^|\\s)” + strClassName + “(\\s|$)”);
var oElement;
for(var i=0; i

SQL Cursors

There is usually a better way than to use cursors, but when it cannot be avoided, template:


DECLARE @myID INT
DECLARE @myID2 INT
DECLARE mycursor CURSOR
  FOR SELECT ID, ID2 FROM MyTable

OPEN mycursor
FETCH NEXT FROM mycursor INTO @myID, @myID2

WHILE @@FETCH_STATUS=0
BEGIN
  -- do stuff
  FETCH NEXT FROM mycursor INTO @myID, @myID2
END

CLOSE mycursor
DEALLOCATE mycursor