This Question is Assumed Answered

1 "correct" answer available (8 pts) 15 "helpful" answers available (5 pts)
16 Replies Last post: Sep 3, 2008 1:01 AM by Rajiv   1 2 Previous Next
Akash Jain Level 3 37 posts since
Feb 28, 2008
Currently Being Moderated

Aug 28, 2008 7:06 AM

How to upload a file on server along with HttpForm?

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

Kamal Bhatt BlackBelt 215 posts since
Oct 17, 2007
Currently Being Moderated
1. Aug 28, 2008 8:46 AM in response to: Akash Jain
Re: How to upload a file on server along with HttpForm?

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.

Duke BlackBelt 294 posts since
Oct 17, 2007
Currently Being Moderated
3. Aug 28, 2008 8:44 PM in response to: Akash Jain
Re: How to upload a file on server along with HttpForm?

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.

Kamal Bhatt BlackBelt 215 posts since
Oct 17, 2007
Currently Being Moderated
4. Aug 28, 2008 10:45 PM in response to: Akash Jain
Re: How to upload a file on server along with HttpForm?

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?

Doug McCrae Level 6 39 posts since
Oct 10, 2007
Currently Being Moderated
7. Aug 29, 2008 8:27 AM in response to: Akash Jain
Re: How to upload a file on server along with HttpForm?

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}

 

Doug McCrae Level 6 39 posts since
Oct 10, 2007
Currently Being Moderated
9. Aug 29, 2008 2:52 PM in response to: Akash Jain
Re: How to upload a file on server along with HttpForm?

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;
?>

 

Duke BlackBelt 294 posts since
Oct 17, 2007
Currently Being Moderated
11. Aug 30, 2008 11:48 AM in response to: Akash Jain
Re: How to upload a file on server along with HttpForm?

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?

Duke BlackBelt 294 posts since
Oct 17, 2007
Currently Being Moderated
12. Aug 30, 2008 11:58 AM in response to: Akash Jain
Re: How to upload a file on server along with HttpForm?

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.

More Like This

  • Retrieving data ...