Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
<%@ WebHandler Language="C#" Class="FileCS" %>


C#
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
public class FileCS : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        int id = int.Parse(context.Request.QueryString["id"]);
        byte[] bytes;
        string contentType;
        string strConnString = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
        string name;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Name, Data, ContentType from Videos1 where id=@Id";
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.Connection = con;
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                bytes = (byte[])sdr["Data"];
                contentType = sdr["ContentType"].ToString();
                name = sdr["Name"].ToString();
                con.Close();
            }
        }
        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
        context.Response.ContentType = contentType;
        context.Response.BinaryWrite(bytes);
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
Posted
Updated 31-Aug-16 2:10am
v2
Comments
CHill60 27-Mar-15 9:02am    
I don't get that error reported with your code. Have you shared the correct code?

Try

C#
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace MyProject
{
    public class FileCS : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // your code
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


You might need to update anything that refers to "FileCS" and make it refer to "MyProject.FileCS" instead.
 
Share this answer
 
v2
Just remove the .cs file which comes with handler and add the code.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900