.Net & SharePoint '07

Technical blog for .Net and all SharePoint 2007 related Information

About the author

Me(Prince) and my wife are B.E in I.T & C.S.E respectively.  I a certified MCPD: Web from 2007 Dec. I am Intrestes in Web Application, MOSS, EPM, etc.
Now working with Deira International School, as IT Application & Help Manager. I have started my career as "Software Developer" @  REACH Sewn Technologies and Consulting Pvt. Ltd, Bangalore India from Oct 2004 to Feb 2006, then as "Web & Intranet Developer" @ Fosroc International Ltd, Dubai from April 2006 to Sep 2009.
You can catch me on mail@jpy-tech.com or mail@princepy.com. Or on 00971 - 50 - 4284530 

Google Translate

Tag cloud

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

RecentComments

Comment RSS

Google Your Location


Downloading Files C#

thanks to http://dotnetslackers.com/community/blogs/haissam/archive/2007/04/03/Downloading-Files-C_2300_.aspx

A lot of questions are being asked about downloading a file from the web server to the client in ASP.NET. I have updated this blog post due to the high number of view & comments. You will realize i added a function called "ReturnExtension" which will return the proper content type and set it to the Response.ContentType property. Almost well known file types are supported.

C# Code

// Get the physical Path of the file(test.doc)
   string filepath = Server.MapPath("test.doc");

   // Create New instance of FileInfo class to get the properties of the file being downloaded
   FileInfo file = new FileInfo(filepath);
 
   // Checking if file exists
   if (file.Exists)
   {
    // Clear the content of the response
    Response.ClearContent();
   
    // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
   
    // Add the file size into the response header
    Response.AddHeader("Content-Length", file.Length.ToString());

    // Set the ContentType
    Response.ContentType = ReturnExtension(file.Extension.ToLower());

    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
    Response.TransmitFile(file.FullName);

    // End the response
    Response.End();
   }

private string ReturnExtension(string fileExtension)
{
     switch (fileExtension)
            {
                case ".htm":
                case ".html":
                case ".log":
                    return "text/HTML";
                case ".txt":
                    return "text/plain";
                case ".doc":
                    return "application/ms-word";
                case ".tiff":
                case ".tif":
                    return "image/tiff";
                case ".asf":
                    return "video/x-ms-asf";
                case ".avi":
                    return "video/avi";
                case ".zip":
                    return "application/zip";
                case ".xls":
                case ".csv":
                    return "application/vnd.ms-excel";
                case ".gif":
                    return "image/gif";
                case ".jpg":
                case "jpeg":
                    return "image/jpeg";
                case ".bmp":
                    return "image/bmp";
                case ".wav":
                    return "audio/wav";
                case ".mp3":
                    return "audio/mpeg3";
                case ".mpg":
                case "mpeg":
                    return "video/mpeg";
                case ".rtf":
                    return "application/rtf";
                case ".asp":
                    return "text/asp";
                case ".pdf":
                    return "application/pdf";
                case ".fdf":
                    return "application/vnd.fdf";
                case ".ppt":
                    return "application/mspowerpoint";
                case ".dwg":
                    return "image/vnd.dwg";
                case ".msg":
                    return "application/msoutlook";
                case ".xml":
                case ".sdxl":
                    return "application/xml";
                case ".xdp":
                    return "application/vnd.adobe.xdp+xml";
                default:
                    return "application/octet-stream";
}

N.B:  If you want to bypass the Open/Save/Cancel dialog you just need to replace LINE1 by the below code

Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name);

Response.TransmitFile VS Response.WriteFile:
 1- TransmitFile: This method sends the file to the client without loading it to the Application memory on the server. It is the ideal way to use it if the file size being download is large.
 2- WriteFile: This method loads the file being download to the server's memory before sending it to the client. If the file size is large, you might the ASPNET worker process might get restarted.

Hope this helps,


Posted by Admin on Saturday, January 22, 2011 3:21 AM
Permalink | Comments (0) | Post RSSRSS comment feed

My Prefered PDC 2008 topics

PDC 2008


Tags:
Posted by Admin on Tuesday, January 04, 2011 10:34 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Tinymce editor Plugin


Posted by Admin on Friday, March 19, 2010 10:54 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Tab control using MultiView and View

Tab control using MultiView and View in asp.net C#

Source : Tab control.aspx (2.38 kb)


Tags: ,
Posted by Admin on Sunday, January 31, 2010 10:33 AM
Permalink | Comments (0) | Post RSSRSS comment feed

ASP.NET Image / Photo Cropper / Resize in C#

thanks to ASP.NET Image / Photo Cropper in C#

You can skip to the demo by clicking here
You can download the source code by clicking here

Sanjay Uttam used javascript obtained from http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/

Source : PhotoCropperASP.NetC#.zip (501.17 kb)

XPS Doc: ASP.NET C# - Image Or Photo Cropping.xps (547.48 kb)

protected byte[] CropImageFile(byte[] imageFile, int targetW, int targetH, int targetX, int targetY)
    {
        MemoryStream imgMemoryStream = new MemoryStream();
        System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));

        Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(72, 72);
  
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        //Adjust settings to make this as high-quality as possible 
        grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        grPhoto.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

        try
        {
            grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH),
                                targetX, targetY, targetW, targetH, GraphicsUnit.Pixel);
            //Only JPG format for this demo
            bmPhoto.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception e)
         ... //removed code to stay concise
        return imgMemoryStream.GetBuffer();
    }

Tags: , ,
Posted by Admin on Monday, January 18, 2010 10:25 PM
Permalink | Comments (0) | Post RSSRSS comment feed

C# 3.0 - Language Enhancements

Auto Implemented Properties

        // Method 1 - Old Method
        private int orderID;
        public int OrderID
        {
            get { return orderID; }
            set { orderID = value; }
        }
        // Method 2 - C# 3.0 and Above
        public int OrderID{ get; set; }
        // Method 3 - ReadOnly
        public int OrderID{ get; private set; }

Local Type Inference (var keyword)

        // Part 1
        // LocalType Inference(also known as "Implicitly TypedLocal Variables)
        // Emphasis on Local, as in Local Scope, 
        var first = 7;
        Console.WriteLine("First variable is of type: {0}", first.GetType());

        int third = first;
        Console.WriteLine("Third variable is of type: {0}", third.GetType());
        // Part 2
        // At first glance, you might think var is the same as type Object
        // But Not. Why?  Because Object will take the int and box it into an object.
        // var INFERS the actual type ... no boxing.  It is type-safe.
        object second = 7;
        Console.WriteLine("Second variable is of type: {0}", second.GetType());
        // Part 3
        // The type is INFERED and can be used like any other type 
        // from that point on:

        var fourth = "Hello world!";
        Console.WriteLine("Fourth variable is of type: {0}", fourth.GetType());
        var fifth = fourth;
        Console.WriteLine("Fifth variable is of type: {0}", fifth.GetType()); 

Object Collection Initializers

    class Order
    {
        public int OrderID { get; set; }
        public DateTime OrderDate { get; set; }
        public string CustomerName { get; set; }
        public decimal OrderAmount { get; set; }
        public List OrderItems { get; set; }
        public Address BillingAddress { get; set; }
        public Address ShippingAddress { get; set; }


        public Order()
        {
            OrderItems = new List();
        }


        public Order(int orderID, DateTime orderDate)
        {
            OrderItems = new List();

            OrderID = orderID;
            OrderDate = orderDate;

        }

    }
    ....
    ....
    // Standard technique for initializing the new instance of a class
    Order order1 = new Order();
    order1.OrderID = 122;
    order1.OrderDate = DateTime.Parse("12/7/2007 7:08 AM");
    // Using an overloaded constructor as a technique
    // for initializing a new instance of a class.
    Order order2 = new Order(123, DateTime.Parse("12/7/2007 8:08 AM"));
    // NEW C# 3.0 object initialization syntax
    // This syntax IMPLICITLY calls the default constructor
    Order order3 = new Order { OrderID = 124, OrderDate = DateTime.Parse("12/7/2007 8:10 AM"), CustomerName = "Bob", OrderAmount = 395.00M };
    // This syntax EXPLICITLY calls the default constructor
    // Can you spot the difference?
    Order order4 = new Order() { OrderID = 124, OrderDate = DateTime.Parse("12/7/2007 8:10 AM"), CustomerName = "Bob", OrderAmount = 395.00M };
    // NESTED OBJECT INITIALIZERS ... demonstrates containment
    Order order5 = new Order()
    {
        BillingAddress = new Address { Address1 = "123 E Main", City = "Burbank", State = "IL", Zip = "60459" },
        ShippingAddress = new Address { Address1 = "456 Walnut", City = "Richardson", State = "TX", Zip = "75088" }
    };
    // COLLECTION INITIALIZERS
    // Works with any type that implements ICollection
    Order order6 = new Order()
    {
        OrderItems = new List {
            new OrderItem { OrderItemID = 1, ProductName = "Foo", Quantity = 3 },
            new OrderItem { OrderItemID = 2, ProductName = "Bar", Quantity = 2 },
            new OrderItem { OrderItemID = 3, ProductName = "Bazquirk", Quantity = 1 }
        }
    };

Tags:
Categories: ASP.Net 3.5 | c#
Posted by Admin on Thursday, November 26, 2009 7:11 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Validation of viewstate MAC failed error

http://blogs.msdn.com/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx


Posted by Admin on Thursday, November 26, 2009 7:07 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Unknown server tag ''asp:ListView'

This error you will get mostly after the installation of .NET Framework 3.5 SP1, then check and place below configuration on web.config.

This can also happen with

  • asp:ListView
  • asp:ScriptManage
  • etc

<assemblies>
    <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

&

<pages>
    <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </controls>
</pages>


Posted by Admin on Tuesday, November 03, 2009 3:38 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Working with Web Resources in ASP.NET 2.0 (WebResource.axd)

http://support.microsoft.com/kb/910442

For more information, visit the following Microsoft Web sites:

 


Linked vs. embedded resources

For more information about Web Resources, see the following blogs by Nikhil Kothari:


Posted by admin on Wednesday, May 27, 2009 11:40 AM
Permalink | Comments (5) | Post RSSRSS comment feed

Formatting Numeric Data

Format PatternNameExample
C or c Currency format $2,003.05
D or d Decimal format
(Works for integers only!)
2,003
E or e Scientific (exponential) format 2.003052e+003
F or f Fixed-point format 2003.05
G or g General format 2003.0515
N or n Number format 2,003.05
P or p Percent format 2,00305.15%
X or x Hexadecimal format
(Works with integers only!)
7D3

Posted by jincy on Wednesday, September 17, 2008 11:11 PM
Permalink | Comments (0) | Post RSSRSS comment feed