Weird WebClient UploadData Problem

G

Guest

Help!

I'm posting a PDF Doc to a remote server using WebClient UploadData and the
following code. The DOC posts fine and the server returns a positive
response. If I access the remote file in Firefox the Adobe PDF Reader in
Firefox kicks off and I can view the file just fine. HOWEVER, if I try to
access the file using Internet Explorer Adobe never kicks off and I receive a
screenfull of text (which looks like the raw text data of the PDF). This
same behavior is seen by anyone accessing the document (so its not a browser
issue on my side). It must be something that I'm not posting correctly -
like the Content-Transfer-Encoding. But for the life of me I can't figure it
out. Any help would be greatly appreciated!

Thanks a bunch!

This is the code:
--------
Sub SendFile
Dim myWebClient As New System.Net.WebClient
Dim body As String
Const boundary as String = "xz9xBzBYzZY"
FileName = "ABC.PDF"

' Read file contents into Byte Array
Dim st As New FileStream(FileName, FileMode.Open)
Dim FileBytes() As Byte
ReDim FileBytes(st.Length)
st.Read(FileBytes, 0, st.Length)
st.Close()

myWebClient.Headers.Add("Accept", "image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, */*")
myWebClient.Headers.Add("Referer", "")
myWebClient.Headers.Add("Accept-Language", "en-us")
myWebClient.Headers.Add("Content-Transfer-Encoding",
"application/octet-stream")
myWebClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" &
boundary)
myWebClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE
6.0;Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)")
myWebClient.Headers.Add("Cache-Control", "no-cache")
myWebClient.Headers.Add("Cookie", "KEY=" & SomeCookie)

' Create Body
body = ""
body = body & "--" & boundary & vbCrLf
body = body & "Content-Transfer-Encoding: binary" & vbCrLf
body = body & "Content-Disposition: form-data; name=""file_1""; filename="""
& FileName & """" & vbCrLf
body = body & "Content-type: application/pdf" & vbCrLf & vbCrLf

dim body2 as String
body2 = "--" & boundary & "--" & vbCrLf

Dim byteBodyArray As Byte()
byteBodyArray = Encoding.default.GetBytes(body)
Dim byteBodyArray2 As Byte()
byteBodyArray2 = Encoding.default.GetBytes(body2)

Dim ByteArrayToSend() As Byte
ReDim ByteArrayToSend(byteBodyArray.Length + FileBytes.Length +
byteBodyArray2.Length)
byteBodyArray.CopyTo(ByteArrayToSend, 0)
FileBytes.CopyTo(ByteArrayToSend, byteBodyArray.Length - 1)
byteBodyArray2.CopyTo(ByteArrayToSend, byteBodyArray.Length +
FileBytes.length - 2)

Dim bResponseArray As Byte() = myWebClient.UploadData(PostURL, "POST",
ByteArrayToSend)

End Sub


--------
 
G

Guest

Yeah - thats what I initially thought but it happens with anyone using IE.
I've tried it with many people.
 
J

Joerg Jooss

bss2004 said:
Help!

I'm posting a PDF Doc to a remote server using WebClient UploadData
and the following code. The DOC posts fine and the server returns a
positive response. If I access the remote file in Firefox the Adobe
PDF Reader in Firefox kicks off and I can view the file just fine.
HOWEVER, if I try to access the file using Internet Explorer Adobe
never kicks off and I receive a screenfull of text (which looks like
the raw text data of the PDF). This same behavior is seen by anyone
accessing the document (so its not a browser issue on my side). It
must be something that I'm not posting correctly - like the
Content-Transfer-Encoding. But for the life of me I can't figure it
out. Any help would be greatly appreciated!

Thanks a bunch!

Two notes:

1. There's no HTTP header Content-Transfer-Encoding. What you want is
"Content-Type".

2. WebClient.UploadFile() does exactly what you're trying to achieve
(although there's a bug in pre .NET 2.0 implementations). Did you try
it?

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top