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@ind y"
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessReques t
context.Response.ContentTy pe = "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.AddWithValu e("@Id", context.Request("Id"))
Using con
con.Open()
Dim file() As Byte = CType(cmd.ExecuteScalar(), Byte())
context.Response.BinaryWri te(file)
End Using
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
<%@ 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@ind
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessReques
context.Response.ContentTy
Dim con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand("SELECT FileBytes FROM dbo.jobreferral WHERE Id=@Id", con)
cmd.Parameters.AddWithValu
Using con
con.Open()
Dim file() As Byte = CType(cmd.ExecuteScalar(),
context.Response.BinaryWri
End Using
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
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("conten t-disposit ion", "attachment; filename=file.txt");
Response.AddHeader("conten
ASKER
do i add it instead of this?
context.Response.ContentTy pe = "application/msword"
context.Response.ContentTy
you can have both
ASKER
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.ProcessReques t
Line 14: Response.AddHeader("conten t-disposit ion", "attachment; filename=file.txt");
Line 15: context.Response.ContentTy pe = "application/msword"
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.ProcessReques
Line 14: Response.AddHeader("conten
Line 15: context.Response.ContentTy
you need to use context.Response
ASKER
where do i add context.Response
so instead of
context.Response.ContentTy pe = "application/msword"
i add
context.Response
so instead of
context.Response.ContentTy
i add
context.Response
context.Response.AddHeader ("content- dispositio n", "attachment; filename=file.txt");
ASKER
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.ProcessReques t
Line 14: context.Response.AddHeader ("content- dispositio n", "attachment; filename=file.txt");
Line 15: context.Response.ContentTy pe = "application/msword"
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.ProcessReques
Line 14: context.Response.AddHeader
Line 15: context.Response.ContentTy
please delete the ';' at end of the line if you use VB...
ASKER
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?
do i make sense?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.