CSharp Notes

1. Internet url regular expression

http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
Http or Https are required, when a lot of internet users don’t even know this protocol exists, as they think every url begins with www.
Some crazy urls (Visit Scotland I’m looking at you!) can have other characters not supported by this expression in them (i.e , or ;).
So in order to get around these little issues I altered the expression a little (see below).
(http(s)?://)?([w-]+.)+[w-]+(/[w- ;,./?%&=]*)?

or

http://([\w-]+\.)+[\w-]+(/[\w– ./?%&=]*)?
REg tool : http://www.c-sharpcorner.com/uploadfile/masief/regularexpvalidator08302005000634am/regularexpvalidator.aspx

———-
http://aspalliance.com/259

Ever try to force the “Download File” dialog in a clients’s browser window when you download files that may be supported with a MIME type? Here is an example that works with many versions of IE.

This example illustrates a simple technique to download files of any type from you web application to the client browser. In addition to setting the MIME types, the example shows how to force the Download File dialog that allows the user to either run or save the file, even if the file type would normally display in the browser’s window.
On my personal site (http://www.sharkcode.com ), I allow the user to download my resume in various formats including Microsoft Word, HTML, Rich Text and Plain Text. I found that for most users (with IE) the correct application was automatically used to view the document from within their browser (such as Word types like .doc). In all cases, however, I wanted the file to be saved to the client’s workstation, rather than simply being displayed by the browser. When I hosted my own site, I could do this by setting the HTTP Headers with “content-disposition” to force the Download File dialog. Now, using the .NET Response object, I can set these headers directly.

The basic ingredients of this example are quite simple and can be found in a Microsoft article at (http://support.microsoft.com/directory/article.asp?ID=KB;EN-US;q306654&lex).

The Microsoft article includes the following code snippet:

private void Page_Load(object sender, System.EventArgs e)
{
  //Set the appropriate ContentType.
  Response.ContentType = “Application/pdf”;
  //Get the physical path to the file.
  string FilePath = MapPath(“acrobat.pdf”);
  //Write the file directly to the HTTP content output stream.
  Response.WriteFile(FilePath);
  Response.End();
}
I used this example to write a simple method that accepts a file name as input and a boolean that will force the File Download dialog which allows the client to either run, or save this file to their local workstation.

private void DownloadFile( string fname, bool forceDownload )
{
  string path = MapPath( fname );
  string name = Path.GetFileName( path );
  string ext = Path.GetExtension( path );
  string type = “”;
  // set known types based on file extension 
  if ( ext != null )
  {
    switch( ext.ToLower() )
    {
    case “.htm”:
    case “.html”:
      type = “text/HTML”;
      break;
 
    case “.txt”:
      type = “text/plain”;
      break;
    
    case “.doc”:
    case “.rtf”:
      type = “Application/msword”;
      break;
    }
  }
  if ( forceDownload )
  {
    Response.AppendHeader( “content-disposition”,
        “attachment; filename=” + name );
  }
  if ( type != “” )  
    Response.ContentType = type;
  Response.WriteFile( path );
  Response.End();   
}
The first few lines of code simply convert the file name argument into a physical path (on the server) and get the various component parts of the name. I only handle the file extension of known types that I’m currently using, but this could be extended to support additional MIME types (see the Microsoft article).

If the forceDownload boolean is true, I use the Response.AppendHeader method to add the “content-disposition” which forces the File Download (save) dialog.

I didn’t find a lot of online help with the “content-disposition” header. In fact, it may even be on its way out in the future. But this currently works fine with IE 6 (and several prior version of IE upon which I’ve tested).
—————–

Merging datasets:

datatable1.Merge(datatable2) .It returns void

———-
ASP.NET File Upload: Workaround for File Size Too Large
add to web.config tag:
<system.web>
<httpRuntime maxRequestLength=”102400″ executionTimeout=”360″/>
</system.web>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: