Hi,
I have a C++ function which sends a post request to a server for uploading a file.
I have also written a server side cgi to receive the data and create the file in a specified directory.
The server side cgi creates a file, however it is blank, I don't see any errors in the log files (apache2/error.log)
Below is my server side script -
!/usr/bin/perl -wT
-
-
use strict;
-
use CGI;
-
use CGI::Carp qw ( fatalsToBrowser );
-
use File::Basename;
-
-
$CGI::POST_MAX = 10240 * 5000;
-
my $safe_filename_characters = "a-zA-Z0-9_.-";
-
my $upload_dir = "/usr/local/web/upload";
-
-
my $query = new CGI;
-
my $filename = $query->param("flat_file");
-
#my $email_address = $query->param("email_address");
-
-
if ( !$filename )
-
{
-
print $query->header ( );
-
print "There was a problem uploading your file (try a smaller file).";
-
exit;
-
}
-
-
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
-
$filename = $name . $extension;
-
$filename =~ tr/ /_/;
-
$filename =~ s/[^$safe_filename_characters]//g;
-
-
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
-
{
-
$filename = $1;
-
}
-
else
-
{
-
die "Filename contains invalid characters";
-
}
-
-
my $upload_filehandle = $query->upload("flat_file");
-
-
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
-
binmode UPLOADFILE;
-
-
while ( <$upload_filehandle> )
-
{
-
print UPLOADFILE;
-
}
-
-
close UPLOADFILE;
-
-
print $query->header ( );
-
Appreciate if anybody can point out what I am doing wrong in the script.
I can post my C++ code also if needed, it's a bit longer so I am not posting it right now.
Thanks,
11 2248 numberwhun 3,509
Recognized Expert Moderator Specialist
You have:
but you aren't telling it what to print. If you want to use the default variable, then put it in there, like so: -
print UPOADFILE ("$_\n");
-
Regards,
Jeff
You have:
but you aren't telling it what to print. If you want to use the default variable, then put it in there, like so: -
print UPOADFILE ("$_\n");
-
Regards,
Jeff
Hi jeff,
Thanks for the quick reply, I modified the script as per your suggestion, still the uploaded file is blank.
Thanks,
Mohit
KevinADC 4,059
Recognized Expert Specialist
what is the enctype of the <form> tag?
what is the enctype of the <form> tag?
Here is my C++ code which sends a post request to the server for file upload. I am using Content-Disposition: multipart/form-data -
int Send_to_DB_via_HTTP(char* Traveller_output_buffer, unsigned data_len, string fileName, string servIP, string url)
-
{
-
-
int sock; /* Socket descriptor */
-
struct sockaddr_in echoServAddr; /* server address */
-
unsigned short echoServPort; /* server port */
-
//char *servIP; /* Server IP address (dotted quad) */
-
//char *url;
-
char *echoString; /* String to send to echo server */
-
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
-
unsigned int echoStringLen; /* Length of string to echo */
-
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
-
and total bytes read */
-
int first_form_len=0;
-
int second_form_len=0;
-
unsigned count = 0;
-
int count1 = 0;
-
int count2 = 0;
-
char buf[2000];
-
int n;
-
const char *m_servIP;
-
const char *m_url;
-
m_servIP = servIP.c_str();
-
m_url = url.c_str();
-
//servIP = "192.168.2.20";
-
echoServPort = 80;
-
//url = "http://tewaris.dyndns.org/";
-
-
/*Create a reliable, stream socket using TCP */
-
-
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
-
cout << " socket () failed" << endl;
-
-
/* Construct the server address structure */
-
-
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
-
echoServAddr.sin_family = PF_INET; /* Internet address family */
-
echoServAddr.sin_addr.s_addr = inet_addr(m_servIP); /* Server IP address */
-
echoServAddr.sin_port = htons(echoServPort); /* Server port */
-
-
/* Establish the connection to the echo server */
-
int k;
-
k = connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr));
-
//cout << k << endl;
-
//if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
-
if (k < 0)
-
{
-
cout<< "Error Connecting to " << servIP << endl;
-
return -1;
-
}
-
else
-
cout << "Connection Successfull to " << servIP << endl;
-
-
//Start the form off with the boundary string
-
-
first_form_len += snprintf(
-
&buffer1[first_form_len],
-
sizeof(buffer1) - first_form_len,
-
"%s%s\r\n",
-
"--",BOUNDARY_STRING );
-
//Some random filename - must end in .dat because web server checks
-
//This is the data portion of the form - the flat file will be sent in this section
-
first_form_len += snprintf(
-
&buffer1[first_form_len],
-
sizeof(buffer1) - first_form_len,
-
"Content-Disposition: form-data; name=\"flat_file\"; filename=\"travellerdata.dat\"\r\n" );
-
-
first_form_len += snprintf(
-
&buffer1[first_form_len],
-
sizeof(buffer1) - first_form_len,
-
"Content-Type: text/plain\r\n\r\n" );
-
-
-
//Prepare the ending part of the form-data - this follows the flat file contents
-
-
-
//Start the form off with the boundary string
-
second_form_len += snprintf(
-
&buffer1[first_form_len+second_form_len],
-
sizeof(buffer1) - (first_form_len+second_form_len),
-
"%s%s\r\n",
-
"--",BOUNDARY_STRING );
-
//Not sure why they have this hidden field, but we need to include it or else
-
//the web server will reject
-
//char system_serial_number[SERIAL_NUMBER_MAX_STRING_LENGTH+1];
-
//Get_serial_number_string( system_serial_number, sizeof(system_serial_number) );
-
second_form_len += snprintf(
-
&buffer1[(first_form_len+second_form_len)],
-
sizeof(buffer1) - (first_form_len+second_form_len),
-
"Content-Disposition: form-data; name=\"filename\"\r\n\r\n%s_auto_end\r\n",
-
syssn_no );
-
-
//End of multi-part form
-
second_form_len += snprintf(
-
&buffer1[(first_form_len+second_form_len)],
-
sizeof(buffer1) - (first_form_len+second_form_len),
-
"%s%s%s\r\n",
-
"--",BOUNDARY_STRING,"--" );
-
-
-
// Put together the headers for HTTP POST
-
count = snprintf(
-
&buffer[0],
-
sizeof(buffer),
-
"POST %s HTTP/1.1\r\n",
-
m_url);
-
/*count = snprintf(
-
&buffer[0],
-
sizeof(buffer),
-
"Connection: %s Keep-Alive\r\n",
-
"");*/
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"Accept-Language: en-us\r\n" );
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"Content-Type: multipart/form-data; boundary=%s\r\n",
-
BOUNDARY_STRING);
-
-
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"User-Agent: Mozilla/3.01 (compatible)\r\n");
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"Host: %s\r\n",
-
m_servIP);
-
-
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"Pragma: no-cache\r\n" );
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"Content-Length: %d\r\n",
-
data_len+first_form_len+second_form_len );
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"Connection: Keep-Alive\r\n" );
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"\r\n" );
-
//cout << buffer << endl;
-
count1 += snprintf(
-
&buffer2[count1],
-
sizeof(buffer2) - count1,
-
"%s%s\r\n",
-
"--",BOUNDARY_STRING);
-
-
count1 += snprintf(
-
&buffer2[count1],
-
sizeof(buffer2) - count1,
-
"Content-Disposition: multipart/form-data; name=\"flat_file\";filename=\"travellerdata.dat\"\r\n",
-
"\r\n");
-
count1 += snprintf(
-
&buffer2[count1],
-
sizeof(buffer2) - count1,
-
"Content-Type: text/plain\r\n");
-
count1 += snprintf(
-
&buffer2[count1],
-
sizeof(buffer2) - count1,
-
"Content-Length: %d\r\n", data_len+count1);
-
count2 += snprintf(
-
&buffer3[count2],
-
sizeof(buffer3) - (count1+count2),
-
"%s%s%s\r\n",
-
"--",BOUNDARY_STRING, "--");
-
-
string status;
-
-
cout << "Sending Post Headers" << endl<< buffer << endl << "End Post Headers" << endl;
-
-
//Send the "POST" header to the HTTP server.
-
if (send(sock, buffer, count, 0) == -1) {
-
-
//status = perror("send");
-
cout << "Stats Worker: received socket error %d on header send\n" << endl;
-
return -1;
-
}
-
/*else
-
{
-
cout << "data Sent" << endl;
-
}*/
-
-
cout << "Now Sending first part of the form data BUFFER1" << endl << buffer1 << endl<< "End First Part"<< endl;
-
-
-
-
if (send(sock, buffer1, first_form_len, 0) == -1) {
-
-
//status = CK_Get_last_error();
-
cout << "Stats Worker: received socket error on header send\n" << endl;
-
return -1;
-
}
-
cout << "Sending start boundary string " << endl << buffer2 << endl;
-
if (send(sock, buffer2, count1, 0) == -1) {
-
-
//status = perror("send");
-
cout << "Stats Worker: received socket error %d on header send\n" << endl;
-
return -1;
-
}
-
cout << "Now Sending file " << Traveller_output_buffer << endl;
-
//Now send the flat file
-
if (send(sock, Traveller_output_buffer, data_len, 0) == -1) {
-
-
//status = CK_Get_last_error();
-
-
cout << "Stats Worker: received socket error %d on message send\n" << endl;
-
return -1;
-
}
-
cout << "Sending ending boundary string " << endl << buffer3 << endl;
-
if (send(sock, buffer3, count2, 0) == -1) {
-
-
//status = perror("send");
-
cout << "Stats Worker: received socket error %d on header send\n" << endl;
-
return -1;
-
}
-
/*cout << "Sending Last part of the form data" << endl << buffer1+first_form_len << endl;
-
-
//Send the last part of the form data to the HTTP server.
-
if (send(sock, buffer1+first_form_len, second_form_len, 0) == -1) {
-
-
//status = CK_Get_last_error();
-
cout << "Stats Worker: received socket error %d on header send\n" << endl;
-
return -1;
-
}*/
-
/*else
-
{
-
cout << "Data has been Sent Successfully" << endl;
-
}*/
-
-
//Receive server response
-
//cout << "Buffer = " << buf << endl;
-
n = recv(sock, buf, 2000, 0);
-
//cout << "Server Response is " << endl << buf << endl;
-
if(n < 0)
-
{
-
cout << "Upload Failed" << endl;
-
return -1;
-
}
-
-
// Make sure this is a HTTP header
-
if (!strstr(buf, "HTTP")) {
-
-
cout << "Stats Worker: response doesn't contain HTTP\n";
-
return -1;
-
}
-
-
if (n < 0) {
-
-
cout << "Stats Worker: response status is " << n;
-
return -1;
-
}
-
-
// while (n > 0) {
-
//printf(buf);
-
if(!strstr(buf, Successful_Upload))
-
{
-
cout << "Traveller Upload Failed. Server Responded " << buf;
-
return -1;
-
}
-
else
-
{
-
cout << "Traveller Upload Successfull. \n Server Response is \n" << buf;
-
//return 0;
-
}
-
//n = recv(sock, buf, 10480, 0);
-
//}
-
-
//close(sock);
-
}
-
Appreciate any help.
Thanks
KevinADC 4,059
Recognized Expert Specialist
I don't know C++. If you are using the correct enctype and the POST method and you are getting no error messages when running the script it is most likely a server issue. Try using a differnt directy to write the file to.
KevinADC 4,059
Recognized Expert Specialist
Are you uploading a file smaller than the post max setting?
$CGI::POST_MAX = 10240 * 5000;
5 megabytes in this case.
Are you uploading a file smaller than the post max setting?
$CGI::POST_MAX = 10240 * 5000;
5 megabytes in this case.
Hi,
Thanks for replying. Yes I am uploading just a 2 megabytes file and it's always going to be this size.
Thanks,
KevinADC 4,059
Recognized Expert Specialist
try writing to a different directory.
try writing to a different directory.
Hi Kevin,
Thanks for all the suggestions. I tried writing to a different directories however no success, still the uploaded file is blank.
I think maybe there is something wrong or missing in my C++ code.
I will try some C++ forums.
Though thanks for all your suggestions.
Thanks,
Mohit
eWish 971
Recognized Expert Contributor
Not sure if this will help, but you could look at an article that was written by a member on how to upload files. Are you trying to upload a text file or image?
You might compare your code.
Just a thought. Also, I suppose that your problem could be coming from the C++.
--Kevin
Not sure if this will help, but you could look at an article that was written by a member on how to upload files. Are you trying to upload a text file or image?
You might compare your code.
Just a thought. Also, I suppose that your problem could be coming from the C++.
--Kevin
Hi Kevin,
Thanks for the reply. I am trying to upload a text file. I already read the article you have pointed to.
Still I am getting a blank file. It seems like the cgi script is not seeing the file data that I am sending through the socket.
Thanks,
Mohit
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: R. Rajesh Jeba Anbiah |
last post by:
Q: How should I handle file upload?
A: File uploading requires HTML form of content type
"multipart/form-data". The file content has to be POSTed/submitted via
the form and once the file is...
|
by: Kevin Ollivier |
last post by:
Hi all,
I've come across a problem that has me stumped, and I thought I'd send
a message to the gurus to see if this makes sense to anyone else. =)
Basically, I'm trying to upload a series of...
|
by: Jonathan |
last post by:
Hi everyone,
I have a problem with the file uploading in Asp.Net and I have read a lot
on forums on this but never found an answer. Here is the problem:
I know Asp.Net maximum Length for...
|
by: Sky Sigal |
last post by:
I have created an IHttpHandler that waits for uploads as attachments for a
webmail interface, and saves it to a directory that is defined in
config.xml.
My question is the following:
assuming...
|
by: Dean Richardson |
last post by:
Hi,
I'm having trouble uploading files via a PHP script. Whenever I upload
a file greater than 10K, the file gets corrupted. However, text files
upload OK.
When I check the FTP Server log I...
| |
by: ganesanji |
last post by:
hi all,
I have written a php coding for uploading a file to a specific folder or location in
server which is a Linux server. I think the coding for file uploaing is correct.
But it does not...
|
by: Atli |
last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t.
In situations where your PHP application...
|
by: LoriFranklin |
last post by:
I'm a bit of a newbie here. I've learned a lot from reading the posts you all have here. I need some help uploading files using an asp form.
I am using some code that I found from Jacob at...
|
by: najmi |
last post by:
hai.
i have one problem that is to upload file..it working perfectly in my computer but fail when deploy at server..the system is to browse the file,then system will zip it before upload it to the...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |