Anant Anand Gupta Let Me Make You Think Like Me

6May/100

How To: Provide Save File Dialog for an Image Request in ASP.Net

It is the default behavior of the web browsers, when you click on the image link, to open the image and display it over there. There are instances when you might want your users to remain on the page and simply download and save the image after clicking on the image link.

This functionality can be achieved in ASP.Net. Here I am going to show how to:

Create a blank ASP.Net Web Application. Now you have to create a folder to store the image to be downloaded and add and image to it.

Your solution should look similar to this:

Open the Default.aspx and create a HyperLink to the image like this:

<asp:HyperLink ID="hlDownloadImage" runat="server" NavigateUrl="~/Images/image001.jpg?Action=Download">Download Image</asp:HyperLink>

Note that the image URL is having additional parameter "Action" with value "Download". We will see the utilization of the same in a moment.

now go and add a new file of Type ASP.Net Module ImageDownload.cs to the solution and modify the code as below:

public class ImageDownload : IHttpModule
{
    #region IHttpModule Members
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }
    #endregion

    public void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpRequest req = app.Request;
        HttpResponse res = app.Response;
        if (req.Params["Action"] != null && req.Params["Action"].ToString() == "Download")
        {
            string path = req.AppRelativeCurrentExecutionFilePath;
            res.ContentType = "image/jpeg";
            res.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(path));
            res.TransmitFile(app.Server.MapPath(path));
            res.End();
        }
    }
}

Modify the web.config file to include following in <httpModules> section:

<add name="ImageDownload" type="WebUploadManager.ImageDownload"/>

You are done. Run the application and click on the Link for the image and you will see the Run/Save dialog. Try removing the Action parameter from the NavigationURL of the hyperlink; image will be displayed on screen as the default behavior of the browser.

Filed under: ASP.Net, C# No Comments
17Jan/101

Windows 7 (Seven) Shortcuts

Here are few very nice and useful shortcuts available in Microsoft's latest operating system Windows 7 (Seven):

Win + Home Minimize all the windows except the current window.
Win + SpaceBar Key Visible windows becomes transparent so you can see the desktop.
Win + Up Arrow Maximize the current window.
Win + Shift + Up Arrow Vertically Maximize the current window.
Win + Down Arrow Restore / Minimize the current window.
Win + Left Arrow Fit window in the left half of the screen.
Win + Right Arrow Fit window in the right half of the screen.
Win + Number (1-9) Open the in the order arranged in the taskbar.
Win + Ctrl + Number (1-9) Toggle the opened windows in same order as they are on taskbar
Win + Alt + Number (1-9) Open Jump List of the apps in the same order as they are on taskbar.
Win + T Focus the taskbar buttons to navigate in.
Win + B Focus the System Tray butoms to navigate in.
Ctrl + Shift + N Create New Folder in explorer or desktop.
Alt + Up Arrow Move up a directory level.
Alt + P Open/Close preview pan in explorer.
Win + P Select the display mode if multiple displays are attached.
Win + Num Pad (+/-) Magnifier Zoom In/Out.
Win + G Navigate in Desktop Gedgets
Win + L Lock Computer (old shortcut)
Win + Tab Windows 3D.
16Jan/100

Design Patterns

What is a Design Pattern?

If you are here reading this post you might be looking a knowledge on Design Patterns and the first question which comes on anyone's mind is "What is a Design Pattern?". Lets answer this question in your own way.

You are person who know the syntax of a programming language and you are able to successfully convert any requirement into the code. One fine morning you reach office and your senior tells you about a new project requirement. You understood the requirement and have decided whats need to be done and what are the object of classes required to achieve this. But internally, whole the time from beginning to the end of the development you always know that there can be a better way to achieve this, and of course you search for other solutions to. Whatever you decide to implement you will finish off the task, but was that the best solution for the requirement? When this question comes in to you mind, the answer can be only given in terms of Design Patterns.

A design Pattern is nothing but a conceptual way to represent a reusable solution for a typical problem.

Here is a list of all known Design Patterns:

  1. Strategy Design Pattern
  2. Decorator Design Pattern
  3. Factory Design Pattern
  4. Observer Design Pattern
  5. Chain of Responsibility Design Pattern
  6. Singleton Design Pattern
  7. Flyweight Design Pattern
  8. Adapter Design Pattern
  9. Facade Design Pattern
  10. Template Design Pattern
  11. Builder Design Pattern
  12. Iterator Design Pattern
  13. Composite Design Pattern
  14. State Design Pattern
  15. Proxy Design Pattern
  16. Command Design Pattern
  17. Mediator Design Pattern
  18. Abstract Factory Design Pattern
  19. Prototype Design Pattern
  20. Bridge Design Pattern
  21. Interpreter Design Pattern
  22. Memento Design Pattern
  23. Visitor Design Pattern
  24. Circular Design Pattern
  25. Double Buffer Design Pattern
  26. Recycle Bin Design Pattern
  27. Model-View-Controller Design Pattern
  28. Model-View-View-Model Design Pattern

I will updating the details of each kind of design patterns as soon as they are ready to be posted.

Filed under: Programming No Comments
11Jun/090

Visual Studio 2005 SP1: didn’t pass the digital signature policy error

To solve this follow the work around:

Work Arround 1:

  1. Start Menu, click Run, type > control admintools and then click OK.
  2. Double-click Local Security Policy.
  3. Click Software Restriction Policies.
    Note: If no software restrictions are listed, right-click Software Restriction Policies, and then click Create New Policy.
  4. Under Object Type, double-click Enforcement.
  5. Click All users except Local Administrators, and then click OK.
  6. Restart the computer.

Install SP1 with no errors.

Work Arround 2:

There is a fix from Microsoft to resolve the issue. Please visit the following knowledge base article:

http://support.microsoft.com/kb/925336 or Download the Update for Windows Server 2003 (KB925336).

Note: Revert the settings after the installation is over.

28May/090

Server.MapPath

In general whenever we need to get physical location of the file in ASP.Net Application, we use Server.MapPath. This is the most commonly adopted method. If you want the file to be located with reference to the path of the current WebPage, then the implementation holds good, but, in case you have to always refer the file from the application root, this method gives you different results. Take the following scenario, where the application directory structure is as follows:

  • Root
    • Data
      • Data.XML
    • ClassA.cs (uses Server.MapPath("\Data\Data.XML"))
    • ClassB.cs (uses ClassA to get the XML file contents)
    • SubDir
      • ClassC.cs (uses ClassA to get the XML file contents)

In the above scenario the ClassC will fail to retrive the contents in case of the WebApplication is hosted in a virtual directory. The application will work fine if it is a website. So the implementation will not show any errors when we run the application from the Visual Studio. to make it more generic we can replace the Server.MapPath with

System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + @"Data\Data.XML".

Page 2 of 3123
Get Adobe Flash playerPlugin by wpburn.com wordpress themes