makam_75
asked on
File upload in cgi
Hi,
I have a web page that uploads a file
<INPUT TYPE="file" NAME="uploaded_file" VALUE="starting value" SIZE=50 MAXLENGTH=80 >
Now, in my cgi, I am trying to retrieve the contents of the file like this
$filename = $query->param('uploaded_fi le');
printf $output "Files contents are ";
while (<$filename>) { printf $output "$_"; }
For text files, the contents do get printed. But for word docs, I am expecting an output like
#######################
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="================ =====_1089 83900==_"
--=====================_10 8983900==_
Content-Type: text/plain; charset="us-ascii"; format=flowed
--=====================_10 8983900==_
Content-Type: application/msword; name="database.doc";
x-mac-type="42494E41"; x-mac-creator="4D535744"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="database.doc"
0M8R4KGxGuEAAAAAAAAAAAAAAA AAAAAAPgAD AP7/CQAGAA AAAAAAAAAA AAABAAAAXQ AAAAAAAAAA
EAAAPwAAAAEAAAD+////AAAAAF 4AAAD///// ////////// ////////// ////////// //////////
////////////////////////// ////////// ////////// ////////// ////////// //////////
////////////////////////// ////////// ////////// ////////// ////////// //////////
////////////////////////// ////////// ////////// ////////// ////////// //////////
////////////////////////// ////////// ////////// ////////// ////////// //////////
#######################
But all I get is
###############
<TY&¨ÿÿÿÿÿÿÿÿÿ
<
<TY&¨ÿÿÿÿÿÿÿÿÿ
<
&
&
<TY&¨ÿÿÿÿÿÿÿÿÿ
###############
Can you tell me what is wrong and how I can read through a word doc and get the binary information?
Thanks.
I have a web page that uploads a file
<INPUT TYPE="file" NAME="uploaded_file" VALUE="starting value" SIZE=50 MAXLENGTH=80 >
Now, in my cgi, I am trying to retrieve the contents of the file like this
$filename = $query->param('uploaded_fi
printf $output "Files contents are ";
while (<$filename>) { printf $output "$_"; }
For text files, the contents do get printed. But for word docs, I am expecting an output like
#######################
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="================
--=====================_10
Content-Type: text/plain; charset="us-ascii"; format=flowed
--=====================_10
Content-Type: application/msword; name="database.doc";
x-mac-type="42494E41"; x-mac-creator="4D535744"
Content-Transfer-Encoding:
Content-Disposition: attachment; filename="database.doc"
0M8R4KGxGuEAAAAAAAAAAAAAAA
EAAAPwAAAAEAAAD+////AAAAAF
//////////////////////////
//////////////////////////
//////////////////////////
//////////////////////////
#######################
But all I get is
###############
<TY&¨ÿÿÿÿÿÿÿÿÿ
<
<TY&¨ÿÿÿÿÿÿÿÿÿ
<
&
&
<TY&¨ÿÿÿÿÿÿÿÿÿ
###############
Can you tell me what is wrong and how I can read through a word doc and get the binary information?
Thanks.
ASKER
Tried like this, no luck yet...
$filename = $query->param('uploaded_fi le');
binmode(STDOUT);
binmode($filename);
printf $output "Files contents are ";
while (<$filename>) { printf $output "$_"; }
printf $output "------------------------- ---------" ;
Also, I notice that the line ---- is not being printed. (The program execution seem to stop at the while loop above.) If anyone can suggest any pointers, I really appreciate it.
Thankyou.
$filename = $query->param('uploaded_fi
binmode(STDOUT);
binmode($filename);
printf $output "Files contents are ";
while (<$filename>) { printf $output "$_"; }
printf $output "-------------------------
Also, I notice that the line ---- is not being printed. (The program execution seem to stop at the while loop above.) If anyone can suggest any pointers, I really appreciate it.
Thankyou.
Try like this.
$filename = $query->param('uploaded_fi le');
binmode(STDOUT);
while (my $bytesread = read($filename, my $buffer, 16384)) {
print $buffer;
}
But why are you trying to print the contents of a Word file, a binary file, to the browser?
$filename = $query->param('uploaded_fi
binmode(STDOUT);
while (my $bytesread = read($filename, my $buffer, 16384)) {
print $buffer;
}
But why are you trying to print the contents of a Word file, a binary file, to the browser?
ASKER
Thank you, but it does not work either.
Here is my code
$filename = $query->param('uploaded_fi le');
binmode(STDOUT);
while (my $bytesread = read($filename, my $buffer, 16384)) {
printf $output "Contents are $buffer";
}
printf $output "------------------------- ---------" ;
And the output I get when I upload a word doc is
Contents are Contents are Contents are Contents are -------------------------- --------
I am creating a web page to accept candidates resumes. Our recuriting application can parse word docs if I pass the binary contents of the file. Thanks for your help.
Here is my code
$filename = $query->param('uploaded_fi
binmode(STDOUT);
while (my $bytesread = read($filename, my $buffer, 16384)) {
printf $output "Contents are $buffer";
}
printf $output "-------------------------
And the output I get when I upload a word doc is
Contents are Contents are Contents are Contents are --------------------------
I am creating a web page to accept candidates resumes. Our recuriting application can parse word docs if I pass the binary contents of the file. Thanks for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
But I want to print to a file, not on the web page.
I had $output = "data.txt";
Anyway I did try
print "Contents are $buffer";
I got "Contents are Contents are Contents are Contents are " printed on my web page.
Thanks.
I had $output = "data.txt";
Anyway I did try
print "Contents are $buffer";
I got "Contents are Contents are Contents are Contents are " printed on my web page.
Thanks.
ASKER
BTW, my form information is
$html_method = "POST";
$action="submit3.cgi";
$encoding="multipart/form- data";
print $query->start_multipart_fo rm($method ,$action,$ encoding);
$html_method = "POST";
$action="submit3.cgi";
$encoding="multipart/form-
print $query->start_multipart_fo
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, I was out sick yesterday, so could not respond to your message.
On doing more research, what I found is:
My $output was mail, meaning I was making the server email the data. like this.
$output = "MAIL";
open (MAIL, "| /usr/lib/sendmail -oi -t");
print $output $buffer;
When I changed the $output to a.txt, it works just fine. a.txt does have all the binary info from the word doc in the way i expected. like this.
open (OUTFILE,">a.txt");
print OUTFILE $buffer;
So looks like when the info get transferred to sendmail, it somehow get changed?
(BTW I am viewing the email through a vi editor, so it is not an email client issue. )
Our recruiting application expects email with the binary content of word doc. so looks like i am stuck.
On doing more research, what I found is:
My $output was mail, meaning I was making the server email the data. like this.
$output = "MAIL";
open (MAIL, "| /usr/lib/sendmail -oi -t");
print $output $buffer;
When I changed the $output to a.txt, it works just fine. a.txt does have all the binary info from the word doc in the way i expected. like this.
open (OUTFILE,">a.txt");
print OUTFILE $buffer;
So looks like when the info get transferred to sendmail, it somehow get changed?
(BTW I am viewing the email through a vi editor, so it is not an email client issue. )
Our recruiting application expects email with the binary content of word doc. so looks like i am stuck.
ASKER
So rj2, you have given the solution for my intial problem, so all the points belong to you.
Can someone tell me how to Accept rj2's answer? I cant find that button anymore .. :(
Can someone tell me how to Accept rj2's answer? I cant find that button anymore .. :(
binmode(STDOUT);
binmode(FILE);
The boundary and content-type stuff will not get printed when you print the content of the file.
That is part of the http headers, not part of the Word file.