By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,009 Members | 2,866 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,009 IT Pros & Developers. It's quick & easy.

Weird WebClient UploadData Problem

P: n/a
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
--------
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
snt
My assumption is that this is something to do with your browser.

snt
http://www.flexoweb.com
http://www.onlinemall.com

Nov 19 '05 #2

P: n/a
Yeah - thats what I initially thought but it happens with anyone using IE.
I've tried it with many people.

"snt" wrote:
My assumption is that this is something to do with your browser.

snt
http://www.flexoweb.com
http://www.onlinemall.com

Nov 19 '05 #3

P: n/a
bss2004 wrote:
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,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.