RSS
15 May 2009

ASP.NET/C# Prompt a Save Dialog Box to Download a File

Author: CoryMathews | Filed under: ASP.NET, Web Development

To save you all (as well as my future self) the trouble of searching all over the place just to find terrible answers on almost every form post out there. Here is a small ASP.NET/C# code snippet that will prompt the user with the save/open dialog box to download a file.

String FileName = "FileName.txt";
String FilePath = "C:/...."; //Replace this
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();

For more content types check our http://en.wikipedia.org/wiki/MIME_type#List_of_common_media_types

If you enjoyed this post, make sure you subscribe to my RSS feed!

24 Apr 2009

Rounded Corners using jQuery

Author: CoryMathews | Filed under: Web Development, jQuery

jQuery is slowly changing the way web designers and developers create web sites. Here is an extremely easy way to achieve rounded corners using jQuery.

You can view live demos on iSearchNotes.com or at the plug-in webpage.

In order to create our rounded corners you will of course first need jQuery. If you do not already have jQuery hurry up and go download it at jquery.com. I will wait.

Waiting…

Ok so now you will also need to download the rounded corners plug-in. This small jQuery script can be downloaded by saving this file jquery.corner.js

Now we have all that will will need to round those corners. On the page you wish to have the rounded corners on you must add includes to both javascript files.

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.corner.js"></script>

Now we must tell jQuery which areas of the layout to round.

<script type="text/javascript">
$(function(){
$("#content").corner()
});
</script>
<div id="content">Hello this would have rounded corners</div>

This simply uses jQuery’s selectors to select the id content and rounds all 4 of that divs corners by the default amount.

This plug-in can do so much more then this example. You can pick which corners to round, how much to round by, how to round, and much, much, more. Instead of listing them all check out the plug-in page to see more uses of it. (there are some pretty cool ones near the bottom of the page)

There are many different ways to use it but some of the more common uses would be:

	$("#content").corner()
	$("#content").corner("bottom");
	$("#content").corner("top");

You could always avoid javascript altogether and do it the hard way using css.

If you enjoyed this post, make sure you subscribe to my RSS feed!

26 Mar 2009

Saving a PHP File as a Word Document

Author: CoryMathews | Filed under: php

So you are looking for a way to save a PHP page as a word doc programatically? Then you have come to the right place.

When I had to overcome this problem my main requirement was that I could not open word on the server.

Searching around the net produced countless pages with the same responses of using COM or using some third party library’s and software. However as with any project if it is possible I, as well as many other developers, try to avoid third party products.

I eventually came up with this solution:

<?php
	header("Content-type: application/vnd.ms-word");
	header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>

Then along with your meta tags add the following

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">

Placing this meta tag on a page will cause the browser to think it is a html word document. Thus prompting you to open/save the document instead of rendering it like a normal webpage.

The only downside to this is that when you open the file in word it will open in the web layout mode of word instead of print layout.

Example Usage:

You can view a demo of this in use over at iSearchNotes.com

Word Page: (SaveAsWordDoc.php)

<?php
	header("Content-type: application/vnd.ms-word");
	header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
<!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>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">
<title>Saves as a Word Doc</title>
</head>
<body>
<h1>Header</h1>
  This text can be seen in word
<ul>
<li>List 1</li>
<li>List 2</li>
</ul>
</body>
</html>

Calling Page:
Just your normal html link.

link

If you enjoyed this post, make sure you subscribe to my RSS feed!

Archives

Blogroll

Misc

Opera
Opera, the fastest and most beautiful browser on the planet. download now