Hi,
we are trying to upload a simple text file over the server using curl, although it seems very simple task it has eat up our brain completely.
same thing we are able to accomplish in HTML within few second and using few line of code.
here is the sample HTML code : working fine uploading file successfully
<HTML>
<HEAD><TITLE>Upload Form</TITLE></HEAD>
<BODY>
<H3>File Uploading Example</H3>
<FORM METHOD="POST" ENCTYPE="multipart/form-data" action="http://www.xyz.com/import.xml?sid=xxxxxxxxxxxxxxxxxxxxxxx">
<P> Text group <BR>
<INPUT TYPE="TEXT" NAME="format" VALUE="csv" SIZE="20"> <BR>
<INPUT TYPE="TEXT" NAME="category" VALUE="Unfiled" SIZE="20"> <BR>
<INPUT TYPE="TEXT" NAME="charset" VALUE="UTF-8" SIZE="20"> <BR>
<P> File list <BR>
<INPUT TYPE="FILE" NAME="file"> <BR>
<P>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<INPUT TYPE="RESET" VALUE="Reset">
</FORM>
<P> Warning! <BR>
This example ignores the HTTP request if the length of the posted data
exceeds 400,000 bytes.
</BODY>
</HTML>
here is the similar curl code: getting exception {Missing file}
{curl 7.0 applet}
{curl-file-attributes character-encoding = "utf8"}
{applet manifest = "manifest.mcurl",
{compiler-directives careful? = true}
}
{let hform:HttpForm = {new HttpForm,
{url "http://www.xyz.com/import.xml?sid=xxxxxxxxxxxxxxxxxxx"} ,
method = HttpRequestMethod.post,
encoding = HttpFormData.multipart-mime-type,
target = "_self",
default-character-encoding = CharEncoding.utf8,
{CommandButton label = "import",
{on Action at c:CommandButton do
{c.form.submit}
}
}
}
}
{hform.add-string-param "format","csv"}
{hform.add-string-param "category","Unfiled"}
{hform.add-string-param "charset","UTF-8"}
{hform.add-file "file",
{url "file:///d:/test.csv"}
}
{value hform}
We tried all the option available with HttpForm
like httpform.add-file or HttpFormData example of curl documentation.
but all the effort are in vain.
any help on this would be great help....thanks in advance..:)
thanks
Akash Jain
So wrote that you are getting a File exception. Are you getting something like this?
FileException: Failed to open file 'd:\test.csv' with mode 'r':
If yes, then kindly check if you have the file that you are adding to submit.
HI kamal,
It is not like this. Its not a FileException. Actually Missing File is server response.
I am giving right path of file. While sending the request every thing is going fine. but other end server is not sble to find the file data or you can say file path.
Regards,
Akash Jain
Have you tried using a packet sniffer to see what is going on? This sounds like a server side problem. Your Curl code looks like the example file upload code you can find at http://www.curl.com/developers/httpforms/file-upload.curl which is a 6.0 applet. You can look at that applet (or any applets code) by using ctrl+right-click and choosing Edit Applet Source with the Curl IDE.
Note that the file name along with the data in the file is sent to the server. So what the server is trying to do with the file name?
hi duke,
thanks for your suggestion
yes, we tried using packet sniffer but its showing, messages are getting posted , could it be any problem with the content-disposition,content-type
or encoding = HttpFormData.multipart-mime-type?
I think this encoding is getting applied to all the param sent across the form.
If it is so then its surely the problem of the curl api.
Server has no problem since html form (as in above post) is getting posted properly without any error.
thanks
Akash J ![]()
Hi kamal,
thanks for reply...
Server is trying to read the file but its not able to fiind the file data sent across the form.
could it be due to content-disposition?
my code (as in above post ) is not working.
sample code given in curl documentation for uploading the file is working fine for there server but its
not working on my server. its too furstrating to me that a simple upload is not working with the curl while same thing working with few lines of HTML code.
Could it be possible that curl api having problem?
Thanks
Akash J ![]()
After changing your example to refer to the server used for the curl dguide example, it worked.
I think that suggests that the request data produced by HttpForm is OK.
{curl 7.0 applet}
{applet {compiler-directives careful? = true}}
{let hform:HttpForm =
{new HttpForm,
||-- {url http://www.xyz.com/import.xml?sid=xxxxxxxxxxxxxxxxxxx} ,
{url http://www.curl.com/cgi-bin/file-upload.pl},
method = HttpRequestMethod.post,
encoding = HttpFormData.multipart-mime-type,
target = _self,
default-character-encoding = CharEncoding.utf8,
{CommandButton label = import,
{on Action at c:CommandButton do
{c.form.submit}
}
}
}
}
{hform.add-string-param format,csv}
{hform.add-string-param category,Unfiled}
{hform.add-string-param charset,UTF-8}
{hform.add-file uploaded-file,
{url test.csv"}
}
{value hform}
Hi dmccrae,
Yes you are right that the url which is provided by the curl displays the Filename with file size and content-type while we upload the file.
But i am saying that the other string parameter is also necessary to the server which is in my above code like "format","category","charset". this string params are the mendatory parametere which server
expects, So i want to send form data and file to the server.
Please suggest me .
Thanks in Advance
Akash Jain ![]()
The reason I tried directing your example to a different server was to check whether the file content was included. I assumed that the other form parameters were sent, but ignored by the server script. However, I did not check that.
By changing the code to use 'submit-open', so the curl applet gets the response, its possible to use the Curl HTTP Monitor, and that does confirm that all the parameters and the file are included in the request.
def reply =
{with-open-streams in = {c.form.submit-open} do
{in.read-one-string}}
{popup-message {pre {value reply}}}
So there must be something different in what is expected by the script on the Curl server and by your server. I'm not sure what to suggest, other than comparing the characters sent by the curl form and the http form. Is that what you meant when asking about 'Content-Disposition' ?
I don't know how the script on the curl server works, so I tried directing it to a php script on localhost that echoes its parameters. (Sorry, my PHP isn't very good.) That can read the uploaded file.
<?php
header( "Content-type: text/xml" );
function getvar($var){
return isset($_POST[$var]) ? $_POST[$var] : '';
}
function getfvar($var,$info){
return isset($_FILES[$var]) ? $_FILES[$var][$info] : '';
}
$format = getvar('format');
$category = getvar('category');
$charset = getvar('charset');
$file_name = getfvar('uploaded-file','name');
$file_type = getfvar('uploaded-file','type');
$file_size = getfvar('uploaded-file','size');
$file_tmp = getfvar('uploaded-file','tmp_name');
$file_text = fread(fopen($file_tmp,'r'),$file_size);
echo <<<QQQ
<?xml version="1.0" encoding="utf-8"?>
<ARGS>
<ARG name='format'>$format</ARG>
<ARG name='category'>$category</ARG>
<ARG name='charset'>$charset</ARG>
<ARG name='file_name'>$file_name</ARG>
<ARG name='file_type'>$file_type</ARG>
<ARG name='file_size'>$file_size</ARG>
<ARG name='file_tmp'>$file_tmp</ARG>
<ARG name='file_text'>$file_text</ARG>
</ARGS>
QQQ;
?>
Thanks for your reply.
I already used this code:
def reply =
{with-open-streams in = {c.form.submit-open} do
{in.read-one-string}}
{popup-message {pre {value reply}}}
and saw the server response that is Missing or invalid file.
But when i am using few line of HTML code its working properly without any server error or any problem.
and i am sure it will work with your php code which you provided me So i can say that its not server problem its related to curl problem. {or in my understanding , how we are using curl code for that}
So this problem frustrate me lot why its not working this problem looks very easy to see or understand but ......
Please give me any suggestion.
Thanks in Advance ![]()
Akash Jain
Which browser and OS are you using for the client? Isn't there a specific file name associated with the message or status of missing file? Is that a 404 error? Or is this a special error page generated by the server which is complaining about the file data being uploaded? What is the server side software - OS and webserver?
Also, is there any authentication involved with this url, which is of the form http://www.xyz.com/import.xml?sid=xxxxxxxxxxxxxxxxxxx
You might try putting {request-browser-resident-http} at the top level of the Curl applet, after the herald, component declaration and the curl-file-attributes, but before any HTTP accesses, including import or include. It is not clear that would make a difference in this case, but if it does, that would be an interesting clue.
Hi Duke,
Thanks for your reply,
I am using Internet Explorer and Mozilla.
Server gives me response like:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE bad SYSTEM "http:/xyz.com/import.dtd">
<!-- operation="import" -->
<bad>Missing or invalid import file.</bad>
when I submit my query by POST method with having some string parameters and file parameter.
Yes it was a special page, Server generates this page, if File sucessful imported then it gives sucessful import the file
otherwise when it missing file it gives me "Missing or Invalid file"
or if some string parameter missing then it gives Missing Parameter in XML call.
Please suggest me.
Regards,
Akash Jain
Hi duke,
{request-browser-resident-http}
This is not working . I dont know what is the problem behind this .
The same, few lines of HTML code is working fine. So i dont think so that its a server Error.
Regards,
Akash Jain
