Automatic File Download

12 02 2011

Troubleshooting automatic file download issue in IE7.0 and later version

Problem: File download using .aspx page or HTTP handler does not work in IE 7.0 and later version.

Solution: If you are storing your files in database and using .aspx page or HTTP handler to write the file to the response, than you must have been bugged by this issue. This is not an error or IE bug but it’s just that the newer version of IE wants to protect its users from malicious contents that could sniff or simply harm your computer or data.
To make it more secure IE has provided option to its users to allow\disallow the automatic downloading of files. This can be set in any kind of security zones. To turn On\Off user needs to perform below steps

Step1) Open IE and click Tools > Internet Options
Step2) Go to Security Tab. Select desired zone and then click Custom Level Button.
Step3) Now look for Downloads > Automatic prompting for file downloads. By default this is disabled due to which any automatic file downloads will be blocked. Enable this feature as shown below.

Note: If you do not want your users to make any changes for above mentioned settings than you can achieve the automatic download but as a tweak you need to have an interim page which requires some user action like button click to download a file.

Advertisements




How to load latest image uploaded with the file upload control without hitting refresh button?

4 03 2010

How to load latest image uploaded with the file upload control without hitting refresh button?

Problem Scenario:

When we use fixed image url and upload an image file with the same name than the image displayed in the browser remains the same as the old one till we go and refresh the page.

The below depicted code will perform according to the above explained scenario.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="FileUpload_Default" %>
<!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 runat="server">
    <title>Image Test</title>
</head>
<body>
 <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
            <br />
            <asp:Image ID="Image1" ImageUrl="~/FileUpload/file1.jpg" runat="server" />
  			</div>
    </form>
</body>
</html>
using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class FileUpload_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string filePath = string.Concat(Server.MapPath("file1.jpg"));

        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(filePath);
        }
    }
}

Solution:

Now to approach this kind of situation their are basically two thing that can be done.

1) Rather than just fixing the image url path of image in .aspx page make the path associated to it in the code behind page as well. Like shown in the below sample code.


protected void btnUpload_Click(object sender, EventArgs e)
    {
        string filePath = string.Concat(Server.MapPath("file1.jpg"));

        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(filePath);
        }

         Image1.ImageUrl = filePath;
    }

OR

2) If you are changing the image url from javascript than just append time or any random number as a part of query string to the image url. Appending the time after the image path will make it treat as a separate request in IIS and the image will be perfectly loaded.


<a href="http://localhost/MyArticles/FileUpload/file1.jpg?t=11/7/2009%204:25:23%20PM">http://localhost/MyArticles/FileUpload/file1.jpg?t=11/7/2009%204:25:23%20PM</a>

or


http://localhost/MyArticles/FileUpload/file1.jpg?t=63245





Choosing storage place for Images

9 10 2008

  

  

Choosing storage place for Images

 

In this article I have jotted down various pros and cons for choosing the best storage place for images used in our web based applications.

 

There are two basic approaches for storing the images that are used in our web application

  1. File System
  2. Database

 

Below are some comparisons between the two:

  

Ease: One can easily configure the path of an image from a directory on the web server containing images while from database one needs to create custom logic for reading Image data (eg .ashx handler for image)

 

 

Flexibility: It is always easy to store a new image file on the web server while in case of database new column needs to be created. 

 

 

Maintenance: Using simple file operations for storing the image on web server is very trivial task while in case of database one might need to create a complex query or business logic.

 

 

Caching: IIS can be easily configured to cache images stored in file system while images in database needs some further complex IIS configuration

 

 

Size: As the web directory in which our application is hosted can be easily scaled in terms of size for storing more images as a when needed while in database the size of database goes on increasing affecting the overall database performance.

 

 

Compatibility: Images are stored on file system with basic file operations while in case of database during migration process to some other database vendor their may be some Data Type compatibility problem

 

 

Performance: File operations are less costly in resource utilization while database operations are more costly in terms of resources.

 

 





How to Paste HTML content in blogspot?

5 06 2008

Problem in pasting HTML content in your blog?

This is because all the blogs does not allow to directly use html in their content area due to security reasons.

The area for writing the content in Blogspot is commonly known as What You See Is What You Get (WYSIWYS) editor.

This WYSIWYG editor interprets the pasted HTML as true HTML. It will render exactly whatever you paste in it as HTML. This might spoil the look and feel of your blog.

Why this happens?

Special characters used for HTML language like <,>,”,& etc should be treated in special manner.
Example:
 < can be treated as <
 > can be treated as >
 ” can be treated as “e;
 & can be treated as &

So writing these special characters in this manner and pasting it in blog can solve your problem. But this may be a tedious task to perform manually.

So here is the solution for it.

Step 1) After making the below code working in .Net 2.0 copy the HTML you want to put in your blog and paste in the text area.

Step 2) Click Encode HTML button.

Step 3) 2nd step will generate the encoded html below the button.

Step 4) Copy the output from 4th step to your blog content (WYSIWYG) editor.

 
Following sample code converts HTML to Encoded HTML format using Code behind Modal in .Net.

<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
    <title>Encode HTML</title>
</head>
<body>
    <form id="form1" runat="server">
<div>
           <asp:TextBox ID="txtHTML" runat="server" Columns="75" Rows="10" TextMode="MultiLine"></asp:TextBox>     
           <asp:Button ID="btnEncode" runat="server" Text="Encode HTML" OnClick="btnEncode_Click" />
           <asp:Literal ID="litEncodedHTML" runat="server" Mode="Encode"></asp:Literal></div>
    </form>
</body>
</html>

 

 

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 

    } 

    protected void btnEncode_Click(object sender, EventArgs e)
    {
        litEncodedHTML.Text = Server.HtmlEncode(txtHTML.Text.Trim());
    }
} 

 

 

Note:

Those who are unaware of .Net they can use the same logic with their know language.

The main crux here is Server.HtmlEncode() method which transforms the HTML to encoded HTML.