.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

File download after login

Posted By:satyapriyanayak       Posted Date: May 02, 2013    Points: 40    Category:    URL: http://www.dotnetspark.com  

File download after login
 

Default.aspx code

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <img  alt="Download pdf" height="22" src="image/pdf.gif" width="23" />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"  
            Text="Download Oracle9i" BackColor="#80FFFF" ForeColor="Black"
            Width="133px" Font-Bold="True"/>

    </div>
    </form>
</body>
</html>

Default.aspx.cs code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default2.aspx?file=" + "Oracle.pdf");

    }
}



Default2.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>


Default2.aspx.cs code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand();
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null && Session["Password"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            string strRequest = Request.QueryString["file"];
            if (strRequest != "")
            {
                //get absolute path of the file
                // the file is passed like a web address /images/myimage.jpg
                string path = Server.MapPath(strRequest); //get file object as FileInfo  
                System.IO.FileInfo file = new System.IO.FileInfo(path); //-- if the file exists on the server  


                if (file.Exists) //set appropriate headers  
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/octet-stream";


                    // write file to browser
                    Response.WriteFile(file.FullName);
                    Response.End();
                }
                else
                {
                    // if file does not exist
                    Response.Write("This file does not exist.");
                }
            }
            else
            {
                //nothing in the URL as HTTP GET
                Response.Write("Please provide a file to download.");
            }
        }
   
    }

    
}


login.aspx code


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

<!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>
    <asp:Label ID="Label1" runat="server" Text="User Name" Width="100px"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="TextBox1" ErrorMessage="Required User name"></asp:RequiredFieldValidator>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Password" Width="100px"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    
        
        
    
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
            ControlToValidate="TextBox2" ErrorMessage="Required Password"></asp:RequiredFieldValidator>
    
        
        
    
    </div>
    <asp:Button ID="Button1" runat="server" Text="Login" onclick="Button1_Click"
        Width="100px" />
    <asp:Label ID="Label3" runat="server"></asp:Label>
    </form>
</body>
</html>


login.aspx.cs code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class login : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand();
    SqlConnection con;

    protected void Button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(connStr);
        con.Open();
        string str = "select UserName,Password from Login";
        cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
          Session["username"] = TextBox1.Text ;
          Session["Password"] = TextBox2.Text;
          while (reader.Read())
        {
            if ((Session["username"].ToString() == reader["UserName"].ToString()) & (Session["Password"].ToString() == reader["Password"].ToString()))
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
               Label3.Text="Invalid username or password";
            }
        }
        
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend