Click here to Skip to main content
15,887,875 members
Articles / Web Development / ASP.NET
Tip/Trick

A solution to problem “Response.TransmitFile(sFullHtmFilePath)” gives unformatted display of htm file on browser on client side

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
3 Dec 2012CPOL5 min read 27.1K   7  
I will give a solution to the following problem "Response.TransmitFile(sFullHtmFilePath)" gives unformatted display of htm file on browser on client side.

Introduction

In this article, I will give a solution to the following problem.

Response.TransmitFile(sFullHtmFilePath)” gives unformulated display of htm file on browser on client side.

Using the code

Developing a web application for some files to be viewed at client machine on client request gives me a little tough time. As in the case of ".htm" file it gives unformatted display of htm file in clients browser. The basic problem was that. How to send/display HTML file with its related resources folder (like CSS file and image files) in same response.

As CSS files have local path in htm pages like:

CSS
<link rel="stylesheet" type="text/css" href="1022_m.css" />

Here is the sample C# code to send a file to client side to be viewed in respective application installed at client side.

C#
switch (System.IO.Path.GetExtension(sFullPath).ToLower())
{
    case ".dwf":
        sContentType = "Application/x-dwf";
        break;
    case ".pdf":
        sContentType = "Application/pdf";
        break;
    case ".doc":
    case ".docx":
        sContentType = "Application/vnd.ms-word";
        break;
    case ".ppt":
    case ".pps":
        sContentType = "Application/vnd.ms-powerpoint";
        break;
    case ".xls":
    case ".xlsx":
        sContentType = "Application/vnd.ms-excel";
        break;
    case ".htm":
    case ".html":
        sContentType = "text/HTML";
        break;
    default:
        sContentType = "Application/octet-stream";
        break;
}

Response.ContentType = sContentType;
Response.Clear();
Response.TransmitFile(sFullPath);
   
Response.End();

Solution

Solution to this problem is "create a virtual directory on an existing Web site to a folder that resides on a remote computer."

A remote virtual directory is a directory that is not contained within the Web site's home directory but appears to client browsers as though it is within the home directory. A remote virtual directory has an alias that is mapped to a Universal Naming Convention (UNC) share location. A client appends the alias to the URL of the Web site to browse the Web content in that virtual directory. The following table illustrates these mappings:

Physical locationAliasURL path
C:\WWWroothome directory
(none)
http://Sales
\\RemoteServer
\SalesData\ProdCustomers
Customershttp://Sales/Customers

Both virtual directories and physical directories (directories without an alias) are listed in Internet Services Manager. A virtual directory is indicated by a folder icon that has a globe in the corner.

How to configure a remote network share

To create a virtual directory to a remote network share, create the share, and then store the Web content in that share. Set the appropriate sharing permissions, and then add the appropriate NTFS permissions to control access to the folder that contains your content.

Note You can also publish Web content to the remote share after the virtual directory is created.

How to create a virtual directory

  1. Log on to the Web server computer using an account that has administrative privileges.
  2. Click Start, point to Programs, point to Administrative Tools, and then click Internet Services Manager.
  3. In the Internet Information Services window, expand * server name (where server name is the name of the server).
  4. Right-click the Web site that you want (for example, Default Web Site), point to New, and then click Virtual Directory.
  5. On the Welcome to the Virtual Directory Creation Wizard page, click Next.
  6. On the Virtual Directory Alias page, type the alias that you want (for example, Sales), and then click Next.
  7. On the Web Site Content Directory page, type the UNC path to the remote folder that you have created (for example, \\Server\Share), and then click Next.
  8. On the User Name and Password page, type the user name and password that has sufficient privileges to gain access to the remote folder.
  9. Note To maintain the highest levels of security, use an account that has the minimum permissions that are necessary to provide access to the remote content.

  10. Click Next, re-type the password that you used in step eight in the Confirm Password dialog box, and then click OK.
  11. On the Access Permissions page, click to select the check boxes of the permissions that you want to set for the virtual directory.
  12. By default, Read permissions and Run scripts permissions are already selected. For example, if you want to allow users to change the content in the virtual directory, click to select the Write check box.

  13. Click Next, and then click Finish.
  14. Note The virtual directory inherits the configuration and security settings of the Web site in which it is created.

How to test the virtual directory

  1. Start Internet Explorer.
  2. In the Address box, type the URL to your Web server (for example, http://WebServer), and then click Go.
  3. Verify that you are able to view the default Web site.

  4. Append the alias of the virtual directory to the address that you typed in step two (for example, http://WebServer/Sales), and then click Go.
  5. The virtual directory Web content is displayed in the browser window.

How to remove a virtual directory

To delete a virtual directory, remove the alias that Internet Information Services (IIS) uses to reference the content stored in that directory.

Note When you delete a virtual directory, the network share and its content are not also deleted.

To delete a virtual directory, follow these steps:

  1. Click Start, point to Programs, point to Administrative Tools, and then click Internet Services Manager.
  2. In the Internet Information Services window, click to expand * server name (where server name is the name of the server).
  3. Expand the Web site that contains the virtual directory that you want to delete. For example, expand Default Web Site.
  4. Right-click the virtual directory that you want (for example, Sales) and then click Delete.
  5. Click Yes when the following message is displayed:
  6. Are you sure you want to delete this item?

    Note: The Web content remains in the remote folder to which the virtual directory was mapped.

  7. Stop, and then restart the website:
    1. Right-click the Web site that you want (for example, Default Web Site), and then click Stop.
    2. Right-click the Web site, and then click Start.
  8. Quit the Internet Information Services snap-in. 

Points of Interest

Solution described in this article may be a solution to other problems facing by you in the "coding life".

This article was originally posted at http://support.microsoft.com/kb/308150

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Systems Limited
Pakistan Pakistan
Author is a student of .Net technologies.

Comments and Discussions

 
-- There are no messages in this forum --