Solved

change filehandler to read more than just word documents

Posted on 2007-03-26
13
343 Views
Last Modified: 2010-05-18
how can i edit my filehandler so it reads doc,pdf, and xls
<%@ WebHandler Language="VB" Class="FileHandler" %>
 
Imports System
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
 
Public Class FileHandler
    Implements IHttpHandler
 
    Const conString As String = "Data Source=JMI-DB03;Initial Catalog=JMI;Persist Security Info=True;User ID=cholo;Password=pele@indy"
 
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "application/msword"
 
        Dim con As SqlConnection = New SqlConnection(conString)
        Dim cmd As SqlCommand = New SqlCommand("SELECT FileBytes FROM dbo.jobreferral WHERE Id=@Id", con)
        cmd.Parameters.AddWithValue("@Id", context.Request("Id"))
        Using con
            con.Open()
            Dim file() As Byte = CType(cmd.ExecuteScalar(), Byte())
            context.Response.BinaryWrite(file)
        End Using
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
 
End Class
0
Comment
Question by:gianitoo
  • 7
  • 6
13 Comments
 
LVL 5

Expert Comment

by:zeroxp
Comment Utility
if you are talking about tell the client how to open pdf and xls files these are the content type you need to set to the response:
application/pdf
application/vnd.ms-excel
to decide which one to use, you may need store document type in you jobreferral DB or decide by original filename.
and of course the client need know how to open these types, otherwise that will become a download dialog.
0
 
LVL 1

Author Comment

by:gianitoo
Comment Utility
how can i just open as a download dialog.  i will have multiple filetypes and having word only will be an issue
0
 
LVL 5

Expert Comment

by:zeroxp
Comment Utility
add this header will force a download dialog
Response.AddHeader("content-disposition", "attachment; filename=file.txt");
0
 
LVL 1

Author Comment

by:gianitoo
Comment Utility
do i add it instead of this?
 
        context.Response.ContentType = "application/msword"
0
 
LVL 5

Expert Comment

by:zeroxp
Comment Utility
you can have both
0
 
LVL 1

Author Comment

by:gianitoo
Comment Utility
i get this
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'Response' is not declared.

Source Error:

 

Line 12:  
Line 13:     Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Line 14:         Response.AddHeader("content-disposition", "attachment; filename=file.txt");
Line 15:         context.Response.ContentType = "application/msword"

 
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Expert Comment

by:zeroxp
Comment Utility
you need to use context.Response
0
 
LVL 1

Author Comment

by:gianitoo
Comment Utility
where do i add context.Response
so instead of
context.Response.ContentType = "application/msword"

i add
context.Response
0
 
LVL 5

Expert Comment

by:zeroxp
Comment Utility
context.Response.AddHeader("content-disposition", "attachment; filename=file.txt");
0
 
LVL 1

Author Comment

by:gianitoo
Comment Utility
i get this
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30037: Character is not valid.

Source Error:

 

Line 12:  
Line 13:     Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Line 14:         context.Response.AddHeader("content-disposition", "attachment; filename=file.txt");
Line 15:         context.Response.ContentType = "application/msword"

 
0
 
LVL 5

Expert Comment

by:zeroxp
Comment Utility
please delete the ';' at end of the line if you use VB...
0
 
LVL 1

Author Comment

by:gianitoo
Comment Utility
zeroxp.   it works but they all open in text file.    i need to open  with their respectives programs.   if pdf  then acrobat.  if word them word  , if excel then excel program.  

do i make sense?
0
 
LVL 5

Accepted Solution

by:
zeroxp earned 500 total points
Comment Utility
as i previously said, you have to decide the file type from DB or some other method.
and when you write that to the client, you need to change the attachment filename accrodingly.
you need to do something similar to this:

      ' fileType is a String
      Dim encoding As New System.Text.ASCIIEncoding()
      Dim someBytes As Byte() = encoding.GetBytes("This is a test. Do not need this if bytes from DB")
      context.Response.Clear()
      Select Case fileType
         Case "TEXT"
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.txt")
            context.Response.ContentType = "text/plain"
         Case "WORD"
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.doc")
            context.Response.ContentType = "application/msword"
         Case "EXCEL"
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.xls")
            context.Response.ContentType = "application/vnd.ms-excel"
         Case "PDF"
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.pdf")
            context.Response.ContentType = "application/pdf"
         Case Else
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.unknown")
            context.Response.ContentType = "application/download"
      End Select
      context.Response.BinaryWrite(someBytes)
      context.Response.End()
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now