473,224 Members | 1,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

PDF request service


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
Nov 23 '05 #1
1 2516
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'

"evilme" <ev****@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...

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

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Doug Farrell | last post by:
Hi everyone, I'm trying to build a program to interface to a SOAP/XML interface provided by one of our vendors. This interface is built with MS SOAP Toolkit 3.0, so I'm guessig they are running...
8
by: turnit \(removethis\) | last post by:
I have a login form that uses the post method to carry the information to the next page. The form works just fine in ie6.0, but fails in mozilla and fails in ie5.2 on a mac. "HTTP/1.1 400 Bad...
1
by: Kevin C | last post by:
When designing an asynchronous service-based system, generally speaking whose responsibility is it to determine when a duplicate request is sent - the system hosting the service or the feeding...
0
by: Matt | last post by:
Hello all, We are using Forms Authentication in an application to protect both sensitive ASP.Net pages and Web services. This question is relating to Web services and forms authentication,...
3
by: CG | last post by:
I have a VS .NET 2003 web service project which references a Managed C++ assembly. The managed C++ assembly in turn uses some unmanaged code. Because of some restriction in the unmanaged...
3
by: Codex Twin | last post by:
Hello apologies if this is the wrong newsgroup to be sending this to. Basically, I have an ASP.NET application that I am trying to force to use a proxy server settings. This can be done by...
4
by: Jit Prasad | last post by:
I have a working client-side Vb.Net (.net v1.0) app that talks to a Webspere webservice on a windows 2000 server. The client side app runs on a citrix server, also a Windows 2000 server. The app...
1
by: jctwguy | last post by:
G'day all My pc is on a domain and I have a test server in a work group. all the web application and the web service are working fine locally, but when I copy the project using service extension...
8
by: Tim Reynolds | last post by:
Our .Net application calls a web method of aplpication 2 that resides on their Apache server. When I as a developer C#, Studios 2003, make the call to their web method from my desktop, I receive no...
6
by: Gibble | last post by:
Hi, We have a javascript on a page that calls a .NET web service. Oddly, in our web servers event logs the following errors kept repeating every minute (that made sense since the web service...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.