I have to do an module program which downloads pdf files in the client side. Currently, when users download the file, the url of the downloaded file is viewed by the user.

Modification instructed:
1. In the page where the file download is requested, the url of the downloaded file is viewed in the status bar of that web page. Modification is that the status bar should not display the url of the downloaded file. -- This had been corrected and the code has modified accordingly. It is done correctly only for the first page. Blinks how to process this for the second page..

2. Currently, the call for the download page is given along with the filename of the user request. The filename is passed as the querystring. Modification needed is that the url should get rewritten as "~/download.aspx" instead of "~/download.aspx?".

I came across using "urlrewriter". But don't know how to implement since i feel little confused with the code examples in net.

I'm placing my code for ur assistance. I'm not using webconfig file also.
old try pdf.aspx
****************

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="old try pdf.aspx.cs" Inherits="old_try_pdf" %>

<!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>Try PDF - Download</title>
    <style>
        .link_text
        {
	        font-family:Tahoma;
	        font-size:10px;
	        color:#1d5296;
	        text-decoration:none;
        }

        .link_text A
        {
	        font-family:Tahoma;
	        font-size:11px;
	        color:#1d5296;
	        text-decoration:underline;	
        }

        .link_text A:hover
        {
	        color:#ff000c;
	        text-decoration:underline;
        }

        .link_text A:active
        {
	        color:#ff000c;
	        text-decoration:underline;
        }
        
        .bullets
        {
	        background-image:url(Bullet.gif);	
	        background-position:center;
	        width:10px;
	        height:10px;
	        display:table-cell;		
	        vertical-align:top;
	        background-repeat:no-repeat;
        }
        
        .display_cell
        {
	        border:solid 1px #e6e7e8;
	        display:table-cell;
	        width:355px;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td class='display_cell'>
                        <asp:Panel runat="server" ID="pnl_pdf"></asp:Panel>
                    </td>
                </tr>
            </table>
            
        </div>
    </form>
</body>
</html>

old try pdf.aspx.cs
*******************

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;
using System.IO;

public partial class old_try_pdf : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Table tb_currency = new Table();
        tb_currency.Width = 355;
        TableRow tr_currency = new TableRow();
        TableCell tc_currency = new TableCell();
        Image img_currency = new Image();
        HyperLink hlk = new HyperLink();

        foreach (string fnames in Directory.GetFiles(@"~\Link 2 Download\pdf"))
        {
            string fname = null;

            tr_currency = new TableRow();

            tc_currency = new TableCell();
            tc_currency.CssClass = "bullets";
            tr_currency.Cells.Add(tc_currency);

            tc_currency = new TableCell();
            tc_currency.HorizontalAlign = HorizontalAlign.Left;
            tc_currency.CssClass = "link_text";
            hlk = new HyperLink();
            hlk.Target = "_blank";

            fname = Path.GetFileNameWithoutExtension(fnames);

            hlk.Text = fname;
            hlk.Attributes.Add("onMouseOver", "javascript:window.status=''; return true;");
            hlk.Attributes.Add("onMouseOut", "javascript:window.status=''; return true;");
            hlk.Attributes.Add("onClick", "javascript:window.status=''; return true;");
            hlk.NavigateUrl = "~/Old download_pdf.aspx?fn=" + fname;
            tc_currency.Controls.Add(hlk);
            tr_currency.Cells.Add(tc_currency);

            tb_currency.Rows.Add(tr_currency);
        }

        pnl_pdf.Controls.Clear();
        pnl_pdf.Controls.Add(tb_currency);

    }
}

Old download_pdf.aspx
*********************

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Old download_pdf.aspx.cs" Inherits="Old_download_pdf" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Old download_pdf.aspx.cs
************************

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;
using System.Net;
using System.IO;


public partial class Old_download_pdf : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a message string
        string strMessage = "Wait.... File is downloading....";
        // Register startup script to show a message in status bar
        this.RegisterStartupScript("SetStatusText", "<script>window.status='" + strMessage + "';</script>");

        if (Request.QueryString.Count > 0)
        {
            string fn = Request.QueryString.Get(0).ToString();

            string spath = @"~\pdf\" + fn + ".pdf";
            Response.ClearContent(); 
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            //Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=my_test.pdf");
           
            Response.TransmitFile(spath);
            Response.End();
        }       

    }
}

Even using register startup variable, in Old_download_pdf.aspx, it is showing the downloaded file url path in status bar of the page Old_download_pdf.aspx. Also we have to rewrite the url from "~/Old download_pdf.aspx?fn=" as simply "~/Old download_pdf.aspx".

When i tried using a hidden variable, i failed in setting the value of the hidden field during the click event of the dynamically creating hyperlink control.


How to complete this? Thanks in advance.

Regards,
M.Sworna Vidhya

Member Avatar for siju kuriakose

Hi,
I think you can hide your statusbar display using Javascript.
Url rewriting is simple and its depends your needs.Try for your download file context.rewrite method in your global.aspx page .But this is a simple soln.it is better use urlrewriter dll and edit path in web config.
Hope this will help you

Member Avatar for simongh2

Your problem is, you're passing the page to download in the url because you're using links, ie a GET request.

try adding a form to your index page. the form should submit to your download page & contain a hidden field. When you click a link in the index page, fire some javascript to set the pdf filename in the hidden field & call submit on the form. the user won't see the filename & the url won't contain it either.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.