Solved

change filehandler to read more than just word documents

Posted on 2007-03-26
13
349 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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