Link to home
Start Free TrialLog in
Avatar of gianitoo
gianitoo

asked on

change filehandler to read more than just word documents

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
Avatar of zeroxp
zeroxp

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.
Avatar of gianitoo

ASKER

how can i just open as a download dialog.  i will have multiple filetypes and having word only will be an issue
add this header will force a download dialog
Response.AddHeader("content-disposition", "attachment; filename=file.txt");
do i add it instead of this?
 
        context.Response.ContentType = "application/msword"
you can have both
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"

 
you need to use context.Response
where do i add context.Response
so instead of
context.Response.ContentType = "application/msword"

i add
context.Response
context.Response.AddHeader("content-disposition", "attachment; filename=file.txt");
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"

 
please delete the ';' at end of the line if you use VB...
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?
ASKER CERTIFIED SOLUTION
Avatar of zeroxp
zeroxp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial