Solved

change filehandler to read more than just word documents

Posted on 2007-03-26
13
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 5

Expert Comment

by:zeroxp
ID: 18797288
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
ID: 18797309
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
ID: 18797317
add this header will force a download dialog
Response.AddHeader("content-disposition", "attachment; filename=file.txt");
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 1

Author Comment

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

Expert Comment

by:zeroxp
ID: 18797393
you can have both
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18800131
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
 
LVL 5

Expert Comment

by:zeroxp
ID: 18804323
you need to use context.Response
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18804782
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
ID: 18804866
context.Response.AddHeader("content-disposition", "attachment; filename=file.txt");
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18826004
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
ID: 18827379
please delete the ';' at end of the line if you use VB...
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18850485
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
ID: 18854890
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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