|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
using HTTP::Request to upload a document
Hi,
Am in the process of automating a monthly document upload to a web database. Am using LWP::UserAgent and HTTP::Request to upload the document somewhat successfully, however there is a small problem and the file is being corrupted slightly. The document seems to be sitting on the server with the correct .doc extension but is not recognised as a word doc. Something at the beginning of the file is amiss I think. My problem is that for some reason the word doc gets displayed as pure text (and various random characters) and at the top of the document is the following: 'ntent-Type: application/octet-stream' Have tried sending a simple text file instead and that also has a similar problem, this time displaying 'ntent-Type: text/plain' at the head of the file. Spent too long on this already. Can anyone point me in the right direction please? Thanks Jon |
|
#2
|
|||
|
|||
|
here is the HTTP request that is generated as a string (this one is the one generated when I upload a basic text file, 9 characters in length).
It all looks fine to me hence my confusion! POST http://www.a_certain_site.com/wibblescript.php Authorization: Basic d2lsYnVyOm9ydmlsbGU= User-Agent: libwww-perl/5.51 Content-Length: 605 Content-Type: multipart/form-data; boundary=xYzZY --xYzZY Content-Disposition: form-data; name="pageAction" upload --xYzZY Content-Disposition: form-data; name="newCode" 23 --xYzZY Content-Disposition: form-data; name="MAX_FILE_SIZE" 10485760 --xYzZY Content-Disposition: form-data; name="userfile"; filename="testfile.txt" Content-Length: 9 Content-Type: text/plain test file --xYzZY Content-Disposition: form-data; name="newTitle" test file title --xYzZY Content-Disposition: form-data; name="newCategory" Web Statistics --xYzZY Content-Disposition: form-data; name="newDescription" test file description --xYzZY-- |
|
#3
|
|||
|
|||
|
anyone?
|
|
#4
|
||||
|
||||
|
It's next to impossible to help you without any code.
Post the code that sends, and conversely the code that accepts the word file upload and maybe something will jump out. Also, we need to know what server environment you're developing for -winblows (joke), or *nix. |
|
#5
|
|||
|
|||
|
ok thanks. thought as the HTTP request seems fine then the code wouldn't be important but here is the uploading code that lives on a windows NT machine (has to!):
Code:
#! d:/perl/bin
use HTTP::Request::Common;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(env_proxy => 1,
keep_alive => 1,
timeout => 120,
agent => 'Mozilla/5.0',
);
my $req = POST 'http://the_site.com/admin/files/index.php',
Content_Type => 'form-data',
Content => [ pageAction => 'upload', newCode => '23', MAX_FILE_SIZE => '10485760', userfile => ['testfile.txt'], newTitle => 'test file title', newCategory => 'Web Statistics', newDescription => 'test file description' ];
$req->authorization_basic('username', 'password');
$ua->request($req);
The server its talking to is unix and the code on there is php. I don't have it but if I manually use the website it works fine. Just thought someone might have had a similar problem cheers Jon |
|
#6
|
||||
|
||||
|
Where did you get that content-type that you're using? I'm almost positive that's your problem.
The correct content type for a posted form with a file upload is: Code:
application/x-www-form-urlencoded There's a short discussion of this at the url below. http://www.perlmonks.com/index.pl?node_id=1751 Next time when you have a question, post your code immediatly, and enclose your code in vBcode code tags. This will save everyone time. |
|
#7
|
|||
|
|||
|
i have to use that content type because I'm uploading a file as well as a bunch of form values.
Have tried your content type but its is ineffective I'm afraid. but I'll check the discussion you mention anyway |
|
#8
|
||||
|
||||
|
Oops. The correct content-type for a multipart form, e.g. one with a file-upload is
Code:
multipart/form-data I'm blissfully ignorant of most of these things, because I just use CGI.pm to generate all of my forms and headers, which it does beautifully. One other thing- given that winblows machines make a distinction between binary and text files, you may need to set "binmode" before you attempt your file upload from your windows box (though you're uploading a text file, right?) I suppose it couldn't hurt, if after fixing the content-type your thingy still doesn't work. |
|
#9
|
|||
|
|||
|
hi. thanks for the input. my perl for windows knowledge is limited
form-data and multipart/form-data seem to be interchangeable. both appear as multipart/form-data once the HTTP request has been built. I need to upload a word doc eventually so I guess the bin thing could be a factor, but yes, I have been reduced to just trying to get a text file up correctly initially as a basic form of debugging. it uploads ok but is just slightly corrupt for some reason. I'll look into the binary aspect next. ok. quick look at binmode. It seems to apply to filehandles which implies to me its more for reading a file rather than sending one. tried uploading a word doc instead of a text file and the HTTP request changed to reflect this, so it can determine automatically that its not a pure text file it seems new segment of HTTP request Code:
--xYzZY Content-Disposition: form-data; name="userfile"; filename="testdoc.doc" Content-Length: 678354 Content-Type: application/octet-stream contents of word file here blah blah anymore ideas? cheers Jon |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > using LWP::UserAgent to upload a word doc |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|