send a word document through POST using VB .net 2005

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey guys, I have a question for you,
I have a setup where I'm sending files through the POST to a php web page, I
read the file contents, put that file contents as text into the POST string,
and send it on it's way.

it works perfectly for text files. However, I can't open a word document
like a text file and have it give me the result I need. I'm looking for a
way to just open a word document and get all the values in it like you would
if you opened it with notepad. any ideas? or any suggestions on how to
better send a non-text file through post? Thanks!
 
You need to perform a http upload, whether from an asp.net page, or from an
application. PHP knows how to accept form data with attachments and word
docs need to go over the wire as complete files or you will lsoe all the
formatting. If you dont need it preserved as a word doc, use word
automation to open the doc and read its contents.
 
Right, but is there some kind of upload function? I need to do this from an
application as It's part of an MS word addin. as of now I'm using
MSXML2.ServerXMLHTTP to send the POST request, but what I can't figure out is
how to send the file over. I'm assuming that the "body" parameter of the
send method is for the POST query string, this might be my problem, but I
can't find any specifics on this.
 
Here's the code I'm using btw:

Dim http As New MSXML2.ServerXMLHTTP

'Create XMLHTTP/ServerXMLHTTP/WinHttprequest object
'You can use any of these three objects.
' http = CreateObject("WinHttp.WinHttprequest.5")
' http = CreateObject("MSXML2.XMLHTTP")
http = CreateObject("MSXML2.ServerXMLHTTP")

'Open URL As POST request
http.open("POST", URL, False)

'Set Content-Type header
http.setRequestHeader("Content-Type", "multipart/form-data;
boundary=" + Boundary)
http.setRequestHeader("name", "datafile")
http.setRequestHeader("filename", "ocxtest.txt")
http.setRequestHeader("Content-Disposition",
"attachment;filename=mitchell-pres.zip")



'Send the form data To URL As POST binary request

Dim d As String




d = "--" + Boundary + vbCrLf + "Content-Disposition: form-data;
name='file';" + " filename=OCXTEST.DOC " + vbCrLf + "Content-Type:
multipart/form-data;" + vbCrLf + vbCrLf
' http.send(d)
http.send(FormData)

d = vbCrLf + "--" + Boundary + "--" + vbCrLf
'http.send(d)


MsgBox(FormData)






'Get a result of the script which has received upload
WinHTTPPostRequest = http.responseText
MsgBox(http.responseText)
 
Hello Aj,
Hey guys, I have a question for you,
I have a setup where I'm sending files through the POST to a php web
page, I
read the file contents, put that file contents as text into the POST
string,
and send it on it's way.
it works perfectly for text files. However, I can't open a word
document like a text file and have it give me the result I need. I'm
looking for a way to just open a word document and get all the values
in it like you would if you opened it with notepad. any ideas? or any
suggestions on how to better send a non-text file through post?

There's really no difference between text and binary data (which is a false
notion anyway): Open the file using a FileStream, load its contents to a
buffer in memory (e.g. a MemoryStream), and post these bytes. If buffernig
in memory isn't required, you can copy the bytes read from the FileStream
directly to your NetworkStream.

Cheers,
 
See, the problem is, when I read the word doc into a filestream,
all I get out of that is:

using .readline 5 or 6 ascii characters. ?'s and blocks.
using .read I get close to the right filesize, but it's all numbers
and using .basestream.readbyte it locks up and stalls on me.

any suggestions?
 
Hello Aj,
See, the problem is, when I read the word doc into a filestream,
all I get out of that is:
using .readline 5 or 6 ascii characters. ?'s and blocks.

Readline() is a StreamReader method. That won't work, as Word files are binary
content.
using .read I get close to the right filesize, but it's all numbers

*Bytes*. But that's what you want! You cannot get a meaningful string representation
of Word document.

Cheers,
 
I guess that's where my confusion is.

So is what your saying, I need to find a way in PHP to convert the bytes to
binary?
 
Hello Aj,
I guess that's where my confusion is.

So is what your saying, I need to find a way in PHP to convert the
bytes to binary?

No, the bytes *are* binary. Just save them in your PHP application to disk
or whatever you need to do.

Cheers,
 
That's what I've been doing, but when I save it as a word doc, and try to
open it up in word, I just get all the bytes as numbers.

Also, if I open a word doc up in note pad pre-sending it has a lot more than
just numbers in it. The word doc that gets sent, ONLY has numbers in it.
 
Back
Top