Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / April 2005

Tip: Looking for answers? Try searching our database.

PDF request service

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
evilme - 13 Apr 2005 03:04 GMT
greetings and salutations, o smarter than i.
i've been working on a solution to secure the delivery of pdfs to client
browsers. we're introducing an public-website element, so i've decided to use
a webservice on the internal application webserver to pass a pdf as a byte
array to a public webserver outside the LAN, where it can be forwarded on the
response to the client browser.

i could use some advice how to improve this: i'm not entirely satisfied with
the memory useage--i'm trying to shy away from taking on making a ISAPI
extention if i can get this efficient enough as is.

more vexingly, every so often, a local user will experience an error where
the PDF is never saved/opened after they make their choice on the file
open/save dialog. further, msie's progress meter remains and looks like it's
very slowly filling (or timing out) even after the pdf has successfully been
saved/opened--is there a final signal i'm failing to send to the browser..?

I designed the web service as follows:

Public Class DocumentRequest
   Inherits System.Web.Services.WebService

   Private EXAMPLE_DIR As String =
Server.mappath("/ExportedPDFs/ExampleSheets/")

   Private Function BinaryGetPDFDocument(ByVal FileDIR As String, ByVal
FileNum As String) As Byte()
       Dim FileStreamIn As New System.IO.FileStream(FileDIR + FileNum +
".pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read,
System.IO.FileShare.Read)
       Dim PDFData(FileStreamIn.Length) As Byte
       Dim i As Integer = FileStreamIn.Read(PDFData, 0,
CInt(FileStreamIn.Length))
       While (i < CInt(FileStreamIn.Length))
           System.Threading.Thread.Sleep(200)
       End While
       FileStreamIn.Close()
       Return PDFData
   End Function

   Private Function CheckExistsPDFDocument(ByVal FileDIR As String, ByVal
FileNum As String) As Boolean
       Try
           If (System.IO.File.Exists(FileDIR + FileNum + ".pdf")) Then
               Return True
           Else
               Return False
           End If
       Catch
           Return False
       End Try
   End Function

   <WebMethod()> _
   Public Function GetExampleSheet(ByVal FileNum As String) As Byte()
       Return BinaryGetPDFDocument(EXAMPLE_DIR, FileNum)
   End Function

   <WebMethod()> _
   Public Function CheckExistsExampleSheet(ByVal FileNum As String) As
Boolean
       Return CheckExistsPDFDocument(EXAMPLE_DIR, FileNum)
   End Function

End Class

and i wrote a consume function as follows:

   Friend Sub PushPDFtoClient(ByRef Response As System.Web.HttpResponse,
ByVal FileNum As String, ByVal TYPE As String)
       Dim ws As New PDFRequest.DocumentRequest
       Dim PDFData() As Byte

       Try
           If (TYPE = "EXAMPLE") Then
               PDFData = ws.GetExampleSheet(FileNum)
           End If
       Catch ex As Exception
          'EDITED - removed some private logging functions
           Response.Write("<script language=""javascript""
type=""text/javascript"">alert('There was an unexpected error accessing the
PDF')</script>")
           Exit Sub
       End Try

       Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(PDFData)
       Response.Clear()
       Response.ContentType = "application/octet-stream"
       Response.AddHeader("Content-Disposition", "attachment; filename=" &
TYPE & "-" & FileNum & ".pdf; size=" & PDFData.Length.ToString)
       Response.AddHeader("Content-Length", PDFData.Length.ToString)
       Response.Flush()

       Dim chunkSize As Integer = 1024
       Dim i As Integer
       For i = 0 To PDFData.Length Step chunkSize

           If (Not Response.IsClientConnected) Then
               Exit For
           End If

           Dim size As Integer = chunkSize
           If (i + chunkSize >= PDFData.Length) Then
               size = (PDFData.Length - i)
           End

           Dim chunk(size - 1) As Byte
           ms.Read(chunk, 0, size)
           Response.BinaryWrite(chunk)
           Response.Flush()
       Next
       ms.Close()
       Response.End()
   End Sub
Random - 13 Apr 2005 20:52 GMT
First, you need to change your line...

Response.ContentType = "application/octet-stream"

.to...

Response.ContentType = "application/pdf"

Then, you have to actually write the byte array to the stream, like so...

Response.BinaryWrite(PDFData)

Then, you finish it off...

Response.Flush()
Response.Close()

If you still have problems, try changing the .AddHeader statement to read
'inline' instead of 'attachment'

> greetings and salutations, o smarter than i.
> i've been working on a solution to secure the delivery of pdfs to client
[quoted text clipped - 118 lines]
>        Response.End()
>    End Sub

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.