Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 23rd, 2001, 05:59 AM
blancbleu blancbleu is offline
wibble
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: UK
Posts: 161 blancbleu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 11 m 49 sec
Reputation Power: 8
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

Reply With Quote
  #2  
Old October 23rd, 2001, 06:27 AM
blancbleu blancbleu is offline
wibble
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: UK
Posts: 161 blancbleu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 11 m 49 sec
Reputation Power: 8
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--

Reply With Quote
  #3  
Old October 23rd, 2001, 12:11 PM
blancbleu blancbleu is offline
wibble
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: UK
Posts: 161 blancbleu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 11 m 49 sec
Reputation Power: 8
Unhappy

anyone?

Reply With Quote
  #4  
Old October 23rd, 2001, 03:24 PM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
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.

Reply With Quote
  #5  
Old October 24th, 2001, 04:18 AM
blancbleu blancbleu is offline
wibble
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: UK
Posts: 161 blancbleu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 11 m 49 sec
Reputation Power: 8
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

Reply With Quote
  #6  
Old October 24th, 2001, 10:02 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
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.

Reply With Quote
  #7  
Old October 24th, 2001, 10:09 AM
blancbleu blancbleu is offline
wibble
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: UK
Posts: 161 blancbleu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 11 m 49 sec
Reputation Power: 8
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

Reply With Quote
  #8  
Old October 24th, 2001, 10:16 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
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.

Reply With Quote
  #9  
Old October 24th, 2001, 10:22 AM
blancbleu blancbleu is offline
wibble
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: UK
Posts: 161 blancbleu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 11 m 49 sec
Reputation Power: 8
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > using LWP::UserAgent to upload a word doc


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT