Thursday, July 31, 2008

ASP.Net Dynamic File Download Protection

I spent a little while today playing with a way to stop users who haven't registered with a site from downloading files. With the help of two examples, I came up with the following. Hope it is useful. Just copy the code by highlighting it, as some of it might slide under the right column (I know, this is a cheesy way to post code, in the queue to do is to widen this blog template and to find a better code poster. Any suggestions for either are appreciated). Anyways, here is the code snippet.

//http://www.codeproject.com/KB/aspnet/SecureFileDownload.aspx
//http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2855600&SiteID=1
if (Request.QueryString["fid"] != null) //&& Session["uid"] != null)
{
string sPath = Server.MapPath("~") + "Your path here";
string sFileName = "";
if (Request.QueryString["fid"].ToString() == "1")
{
sPath +=
"real_file_name.config";
sFileName =
"display_file_name.pdf"; //or .doc, .jpg, .mp3, whatever
Response.AddHeader("content-disposition", "attachment; filename=" + sFileName);
Response.ContentType =
"application/octet-stream";
System.IO.
FileStream fs =
System.IO.
File.Open(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
Byte[] flen = new byte[256];
int iBytes = fs.Read(flen, 0, flen.Length);
Response.Buffer =
true;
Response.BinaryWrite(flen);
while (iBytes != 0)
{
iBytes = fs.Read(flen, 0, flen.Length);
Response.BinaryWrite(flen);
}
fs.Close();
Response.End();
}


Place this in either your Page_Init or Page_Onload, adjust the paths, names, and what not and you have your file protection (in the snippet the session check is commented out, but you can see what I was getting at). For your base file path you can use Server.MapPath("~") + "/whatevedirectory/". Also if you are writting to files, it is a good idea to keep these files in your App_Data directory, as I've heard (don't know if it is true) that messing with files outside that directory may cause your app to recycle.

The above code could probably be put into an HTTPModule, you could read real file names and display names from a database, or encrypt your query string, whatever, this will give you the basics.

Column Side Gradients

I suck at Photoshop, not because I really suck, but because I don't get much time in on the design side. Too bad because I enjoy the design side a lot. I think ultimately I'd like to phase back on the coding side and spend more time on the design side. I figure the programming spectrum goes like this-

  • Blur between Web Application Developer and Hard Core Low Level Programmer
  • Web Application Developer
  • Web Scripter
  • Web Designer
  • HTML Content Developer

I think I'd like to find myself in the "Web Scripter" category, someone who does back-end database programming, but also does design work. I think I could meet a lot of small business user needs that way. Small business owners don't need or care about the craft of programming, and they can't support the budget for solutions based on those methodologies, but small business owners do need interactivity and data storing/retrieval. A nice blend of task for a single coder or a small team. I think I'm getting burned out a little bit on the application development side of things. I see a lot of extra abstraction layers being added into the mix, which is all well in good, but often times the extra effort is unnecessary and it is only done to please the herd.

Anyway, that is neither here nor there. Here is a link to a Photoshop/CSS tutorial on how to create gradient fades on the sides of content columns, something I'm seeing a lot of these days.

http://www.projectseven.com/tutorials/images/gradient_tiles2/index.htm

A Bunch of Techie Links...

Again, another hurry up and wait day at work, even though I do have some things to do, I took a few minutes to go through my starred google feeds, and I present to you all (and to myself in the future) a bunch of links that might be useful to you.

Virtualization: http://virtualbox.org/
Plots: http://www.deensoft.com/lab/protochart/
Software: http://cyn.in
Jquery Plotting: http://code.google.com/p/flot/
Flot example: http://www.garyrgilbert.com/blog/index.cfm/2007/12/12/JQuery-Charting-Plugin
Another jQuery Plotter: http://www.reach1to1.com/sandbox/jquery/jqchart/
Httpmodule as Background Service: http://dotnetslackers.com/ASP_NET/re-126158_Using_an_HttpModule_to_Run_a_Background_Service.aspx
Resources for Photoshop: http://amarieveanne-stock.deviantart.com/art/Dragons-volume-II-76024664
ProtoChart: http://www.deensoft.com/lab/protochart/dynamic.php

Monday, July 28, 2008

The New Look And Feel

I found a pretty decent article on Web 2.0 design principles, check it out for yourself. Scroll down a bit, the site is a bit add heavy towards the top.

http://www.masternewmedia.org/information_design/information-design-principles/web-20-design-simple-social-design-components-20071017.htm

Highlights include-

- Centered Design
- Rounded Edges
- Large Fonts
- Simple, Consistent Navigation
- Logos and Icons
- Subtle 3D/ Refective Surfaces
- Whitespaces
- A whole section on the socal web.

Saturday, July 26, 2008

California Bans Transfat, Freedom

Mommy, otherwise known as the government to most Californians, has recently passed a law banning trans fat from the state. While I think most Americans eat too much trans fat, I don't think the government has any business stepping into the role of Mommy. I'm down with having people who eat to much trans fats and thus suffer health related issues due to their over consumption of trans fat paying a higher premium for health care (and I would be one of those individuals, unfortunately). I don't believe liberals through government agencies should have the right to make sure people "live correctly". My ancestors bailed Europe because they didn't believe in government telling people how to live, and the setup this great country known as the United States of America where people are free to make their own choices, be they good or bad, and benefit or suffer the consequences of their own choices, again be they good or bad. It sets a really bad precedent abdicating even small personal freedoms (even to eat "the wrong" foods) to government. Today trans fat, but what will "Mommy" try to fix for us tomorrow? Ten years from now? Pretty soon government will be trying to make sure we all "believe correctly" and help us all reach acceptable ideological conclusions for the good of the "community" (i.e. the government). F that. Live Fat or Die! (j/k). How about live free of lefties (and conservatives) in government telling us how to live, and all of us being responsible for our own life choices.

http://abcnews.go.com/print?id=5393222

Friday, July 25, 2008

Flash 3D

I don't know HOW these guys did it, but they did. Here is a real time 3D Flash Engine. Yes...3D...in Flash, not Director, Flash.

http://www.electricoyster.com/

[Update]

Here might be what these guys are using for their 3D Engine-

http://www.flashsandy.org/

Thursday, July 24, 2008

jQuery Autocomplete example

I really dig jQuery, so my experimentation with it continues. I found an excellent jQuery based autocompletion plugin from here

http://www.pengoworks.com/workshop/jquery/autocomplete.htm

I created a quick and dirty aspx "servlet" page, with all the

aspx-

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoCompleteTest.aspx.cs"
Inherits="jQueryAutoComplete.servlets.AutoCompleteTest" %>


Code Behind- (mine was hooked into an access db...)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


namespace jQueryAutoComplete.servlets
{
public partial class AutoCompleteTest : System.Web.UI.Page
{
protected void Page_Init ( object sender, EventArgs e )
{
if ( Request.QueryString["q"] != null )
{
string sQry = "Select [CatID], [CatDesc] from tCategories where " +
"[CatDesc] like '" + Request.QueryString[ "q" ] + "%' order by [CatDesc]";
string oStr = ConfigurationManager.ConnectionStrings[ "cats" ].ToString ();
System.Data.OleDb.
OleDbConnection oConn =
new System.Data.OleDb.OleDbConnection(oStr);
oConn.Open();
System.Data.OleDb.
OleDbCommand oCmd =
new System.Data.OleDb.OleDbCommand ( sQry, oConn );
System.Data.OleDb.
OleDbDataReader oRdr =
oCmd.ExecuteReader (
CommandBehavior.CloseConnection );
while ( oRdr.Read () )
{
Response.Write ( oRdr[
"CatDesc" ].ToString () + "|" +
oRdr[
"CatID" ].ToString ()+ Environment.NewLine);
}
oRdr.Dispose ();
oCmd.Dispose ();
oConn.Dispose ();
}
Response.End ();
}
}
}


And then my HTML page, which contains the simple autocomplete box which is pretty much pulled directly from the above site-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<
html>
<
head>
<title>jQuery Autocomplete</title>
<script src="jquery-1.2.6.pack.js" type="text/javascript"></script>
<script type='text/javascript' src='jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
</
head>
<
body>
<
form action="" onsubmit="return false;">
<p>
Ajax City Autocomplete:
<input type="text" id="Names" value="" style="width: 200px;" />
<input type="button" value="Get Value" onclick="lookupAjax();" />
</p>
</
form>
<
script type="text/javascript">
function
findValue(li) {
if( li == null ) return alert("No match!");
// if coming from an AJAX call, let's use the CityId as the value
if( !!li.extra ) var sValue = li.extra[0];
// otherwise, let's just display the value in the text box
else var sValue = li.selectValue;
alert(
"The value you selected was: " + sValue);
}

function selectItem(li) {
findValue(li);
}

function formatItem(row) {
return row[0] + " (id: " + row[1] + ")";
}

function lookupAjax(){
var oSuggest = $("#Names")[0].autocompleter;
oSuggest.findValue();
return false;
}

$(document).ready(
function() {
$(
"#Names").autocomplete(
"AutoCompleteTest.aspx",
{
delay:5,
minChars:1,
matchSubset:1,
matchContains:1,
cacheLength:10,
onItemSelect:selectItem,
onFindValue:findValue,
formatItem:formatItem,
autoFill:
true
}
);
});
</script>
</
body>
</
html>


And wa-la! Autocomplete on the fly. Thank you pengoworks.com and jQuery.

On Aliens

Sometimes when I chat with people about UFO's and such (by the way I don't believe that the Earth is being visited by Aliens, I think something much more sinister is going on then that), one of the immediate fall back positions that people often use to discredit what I will call "the phenomena" goes along these lines...

"If there really where aliens visiting us, and the government knew about it, someone would leak."

My response is often that there are people who leak. Here is a recent example-

http://www.news.com.au/story/0,23599,24070088-13762,00.html

Again, I think UFO's and Alien Abduction type stuff fall into the spiritual realm. In my opinion if anyone takes a serious look at the phenomena, they will not be able to easily discredit the notion that something (or somethings) is going on, what that something is (or somethings are) is what the serious debate is all about.

Friday, July 18, 2008

jQuery Alternating Row Highlighter Example

Again, another simple use of jQuery to highlight rows in a table, but this example keeps the different classes for alternating table rows intact. Again, I'm still learing, I bet there is some kind of xpath test for values that can cut down the size of the javascript.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<title>Alternating Highlights</title>
<script src="../s/jquery-1.2.6.pack.js" type="text/javascript">
</script>
<style type="text/css">
.r0 {background-color:White;color:Black}
.r1 {background-color:#f5f5f5;color:Black}
.hl {background-color:Yellow;color:red;cursor: hand}
</style>
</
head>
<
body>
<
p>
jQuery alternating highlight example.
</p>
<
table id="test" border="1" rules="none" cellspacing="0"
style="border: solid 2px navy">
<
tr class="r0">
<td>some data</td>
<td>some data</td>
</
tr>
<
tr class="r1">
<td>some other data</td>
<td>some other data</td>
</
tr>
<
tr class="r0">
<td>some data</td>
<td>some data</td>
</
tr>
<
tr class="r1">
<td>some other data</td>
<td>some other data</td>
</
tr>
<
tr class="r0">
<td>some data</td>
<td>some data</td>
</
tr>
<
tr class="r1">
<td>some other data</td>
<td>some other data</td>
</
tr>
<
tr class="r0">
<td>some data</td>
<td>some data</td>
</
tr>
<
tr class="r1">
<td>some other data</td>
<td>some other data</td>
</
tr>
</
table>
<
script language="javascript" type="text/javascript">
$(document).ready(function() {
$(
"#test").find("tr[@class='r0']").each(
function()
{
$(
this).hover(function() {
$(
this).addClass("hl");
},
function(){
$(
this).removeClass("hl");
$(
this).addClass("r0");
});
});
$(
"#test").find("tr[@class='r1']").each(
function()
{
$(
this).hover(function() {
$(
this).addClass("hl");
},
function(){
$(
this).removeClass("hl");
$(
this).addClass("r1");
});
});
});
</script>
</
body>
</
html>

Wednesday, July 16, 2008

jQuery Attribute-Based Form Validation Example

Ok, this code is rough, and I'm sure this has been done before, but while playing around with jQuery I thought it might be interesting to try out some form validation by just adding an additional attribute to the input type, then having jQuery search through all the form elements, and then based on that added attribute, test the form values for correctness. Below is a really crude example that demos this technique. It works, I'm not sure if some xhtml validators will squak because of the extra attribute. Anyway, something to play with. One jQuery plugin, one error object (not in this demo) with an array of error strings, and wallah!, form validation. This example will get you thinking.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<title>Untitled Page</title>
<script src="jquery-1.2.6.pack.js" type="text/javascript"></script>
<style type="text/css">
.red {border-color:Red;background-color:Yellow}
</style>
</
head>
<
body>
<
form name="myform">
<
table>
<tr>
<td>Required Numeric1</td>
<td>
<input type="text" id="num1" name="NumericField" v="RN" maxlength="3" />
</td>
</tr>
<tr>
<td>Required Numeric2</td>
<td>
<input type="text" id="num2" name="NumericField2" v="RN" maxlength="3" />
</td>
</tr>
<tr>
<td>Required Numeric3</td>
<td>
<input type="text" id="num3" name="NumericField3" v="RN" maxlength="3" />
</td>
</tr>
<tr>
<td>Numeric4</td>
<td>
<input type="text" id="num4" name="NumericField4" v="N" maxlength="10" />
</td>
</tr>
</
table>
<
input type="button" value="Validate" onclick="return vF('myform');" />
</
form>
<
script language="javascript" type="text/javascript">
var bR = false;
$(document).ready(
function() {
bR=
true;
});
function vF(f)
{
try
{
var sErr = "";
var fid = "";
if(bR==false)return;
$(
"form").find("input[@v='RN']").each(
function()
{
if (isNaN(this.value) this.value=="")
{
sErr+=
"* Required numeric value for " + this.name + ".\n";
this.value = "";
fid = (fid ==
"" ? this.id : fid);
$(
"#"+this.id).addClass("red");
}
else {$("#"+this.id).removeClass("red");}
});
$(
"form").find("input[@v='N']").each(
function()
{
if (isNaN(this.value))
{
sErr+=
"* Numeric value required for " + this.name + ".\n";
this.value = "";
fid = (fid ==
"" ? this.id : fid);
$(
"#"+this.id).addClass("red");
}
else {$("#"+this.id).removeClass("red");}
});
if(sErr != "")
{
sErr =
"Please Correct the following issues-\n" + sErr;
alert(sErr);
$(
"#" + fid).focus();
return false;
}
return true;
}
catch(err)
{
alert(err);
}
}
</script>
</
body>
</
html>

Monday, July 14, 2008

Jquery Check All

Another tutorial example, others have posted more elegant solutions, but here is my example.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<title>Untitled Page</title>
<script src="../s/jquery-1.2.6.pack.js" type="text/javascript"></script>
</
head>
<
body>
<p>
check all/none example.
</p>
<p id="test">
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<br />
<input type="button" value="Check All" onclick="CA(1);" />
<input type="button" value="Check None" onclick="CA(0);" />
</p>
</
body>
<
script language="javascript" type="text/javascript">
function
CA(w){
$(
"#test").find("input[@type$='checkbox']").each(function() {
this.checked = (w == 0) ? false : true;
});
}
</script>
</
html>

Design Pattern Links

I was in a holding pattern today, so I did a little reading on design patterns. Here are a few links I found that are worth reading.

http://www.developer.com/design/article.php/1502691

http://www.go4expert.com/forums/showthread.php?t=5127

And, for fun, I guess now everything is a pattern. I guess in the truest sense this is correct, but maybe calling everything a pattern is overkill.

http://www.welie.com/patterns/

Jquery Basic Example: Highlighting Table Rows

This is pulled pretty much directly from the second tutorial on Jquery's site. With big data grids sometimes adding a row highlighter allows users to keep track of where they reading in the table. I've been doing this type of stuff with javascript for a long time, but JQuery just made this technique uber simple. Here is an example. Note, some of the code might slide under the right side bar, just copy and past the whole code block and it will get the covered code if you are interested. Also you will need to adjust the link at the top to where your jquery code resides. Now the code-


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<title>Untitled Page</title>
<script src="../s/jquery-1.2.6.pack.js" type="text/javascript"></script>
<style type="text/css">
.hl {color:red; background-color:yellow}
</style>
</
head>
<
body>
<
p>
Low Cost Table Row Hover Event
</p>
<table id="test" cellpadding="2" cellspacing="0" border="0"
bgcolor="#f5f5f5">
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>stuff</td><td>stuff</td></tr>
</table>
<
script language="javascript" type="text/javascript">
$(document).ready(function() {
$(
"#test tr").hover(function() {
$(
this).addClass("hl");
},
function(){
$(
this).removeClass("hl");
});
});
</script>
</
body>
</
html>

JQuery 1.2.6 Intellisense

Some bright folks have cobbled together a Jquery file (1.2.6 release) that Visual Studio 2008 can parse, allowing for intellisense for Jquery in Visual Studio. Pretty cool.

Download and read onward here-

http://www.mustafaozcan.net/en/post/2008/06/15/JQuery-1-2-6-Intellisense-for-Visual-Studio-2008.aspx

Wednesday, July 09, 2008

Google's New Virtual World

The competition for who can create a "metaverse" gets a little more intense.

Check out Google's "Lively" virtual world.

http://www.lively.com/html/landing.html

Read a synopsis here-


[Update 11/20/08]
Looks like Google's Lively was short lived. The plug will be pulled soon.

Saturday, July 05, 2008

Islam Rising: School Infiltration

Here is an interesting article from WND about how Islamist, with the help of liberal allies, are infultrating Western schools.

http://www.wnd.com/index.php?fa=PAGE.view&pageId=68785

Thursday, July 03, 2008

OO Class Keywords and What They Do

Ok, I come from a scripting web background, and even to this day some OOP stuff I either forget or have never officially "learned." I found a good link talking about the various forms of classes in OOP, might be useful.

http://www.functionx.com/csharp2/classes2/abstraction.htm

Picture of the Week: Chinese Troops on Segways

They are not just for gadget prone middle aged engineering types anymore!


Chinese anti-terrorist troops on Segway's. Click the photo to read more.

Tuesday, July 01, 2008

Bad @ss, TCP/IP from a Web Browser in JavaScript

Welcome to the world of "Comet" servers. What is a Comet server? A Comet server is a server that supports communicating via TCP and other protocols directly to and from a web server from a web browser using client side Javascript. Typically you could enable doing this by using a Java applet or a Flash object, but javascript I guess has matured enough to the point where this can be done, in theory, without the use of external plugins.

Read on [HERE] for more info about Comet servers.

Below is a great comet server called Orbital that is open source, plays well with both IIS and Apache, and is easy to use. Read about it here-

http://cometdaily.com/2008/07/01/sockets-in-the-browser/

Here is just a taste of how easy it is to use (allegedly, I haven't installed it yet)-

var conn = new TCPSocket(hostname, port)
conn.onopen = function() { alert('connection opened!') }
conn.onread = function(data) { alert('RECEIVE: ' + data) }
conn.onclose = function(data) { alert('connection closed!') }
conn.send('Hello World');

Wow. Chat. MMO's. Ditching bloated complex web services as a means of communication. If Orbital works as advertised, I see whole new worlds opening up.

Orbital's website is here-

http://orbited.org/

I found this post on Ajaxian, an excellent blog well worth an RSS subscription-

http://ajaxian.com/archives/tcpsocket-sockets-in-the-browser