Solved

change filehandler to read more than just word documents

Posted on 2007-03-26
13
350 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validation for Passport expiry in asp.net 2 32
asp.net mvc5 6 34
async questions 5 49
How do you think this website does searches? 5 36
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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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