cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: File uploading problem

From: Amit Nargund <amit.snargund_at_gmail.com>
Date: Mon, 6 Oct 2008 17:56:02 +0530

On Sat, Oct 4, 2008 at 8:01 PM, Amit Nargund <amit.snargund_at_gmail.com>wrote:

> Hi,
>
> I am currently facing some problems in uploading the file using curl in
> webkit....
> I am using rapidshare.com for testing this code, and I am uploading file
> of around 30-50 bytes.
>
> In this before posting a request I m calculating the size of the request.
> After calculating the size of the total contents, all the attributes are set
> that are required.
>
> The problem is, the site on which I am trying this code, does not respond.
> It still waits as if the site is expecting the more data.
>
>
> Here is the code snippet...
>
> //calculate size of the request
> curl_off_t size = 0;
> bool chunkedTransfer = false;
> for (size_t i = 0; i < numElements; i++) {
> FormDataElement element = elements[i];
> if (element.m_type == FormDataElement::encodedFile) {
> long long fileSizeResult;
> if (getFileSize(element.m_filename, fileSizeResult)) {
> printf("\ngetfile size of %d\n",i);
> if (fileSizeResult > maxCurlOffT) {
> // File size is too big for specifying it to cURL
> chunkedTransfer = true;
> printf("\ninside if chunkedTransfer %d for element
> %d\n",chunkedTransfer,i);
> break;
> }
> size += fileSizeResult;
> } else {
> chunkedTransfer = true;
> break;
> }
> } else
> size += elements[i].m_data.size();
> }
> //size calculation done.
> curl_easy_setopt(d->m_handle, CURLOPT_POST, TRUE);
>
>
>
> // cURL guesses that we want chunked encoding as long as we specify the
> header
> if (chunkedTransfer)
> *headers = curl_slist_append(*headers, "Transfer-Encoding:
> chunked");
> else
> curl_easy_setopt(d->m_handle, CURLOPT_POSTFIELDSIZE, size);
>
> curl_easy_setopt(d->m_handle, CURLOPT_READFUNCTION, readCallback);
> curl_easy_setopt(d->m_handle, CURLOPT_READFUNCTION, job);
>
> I also checked in readCallback, it reads the give file properly and returns
> the appropriated file size.
> Also I checked the http traffic through Wireshark. What I observed is there
> in no POST packet observed!
>
>
> Please provide some solution. I wonder if there are any more parameters
> that I need to set!
>

On Sat, Oct 4, 2008 at 8:38 PM, Jeff Pohlmeyer <yetanothergeek_at_gmail.com>wrote:

> On Sat, Oct 4, 2008 at 9:31 AM, Amit Nargund <amit.snargund_at_gmail.com>
> wrote:
>
> > I am currently facing some problems in uploading the file
> > using curl in webkit....
>
> For multipart/formdata type file uploads, you might want to
> check out CURLOPT_HTTPPOST instead of CURLOPT_POST.
>
>
> If you can upload succesfully from Firefox, the "LiveHTTPHeaders"
> browser extension might also give you some clues, as well as the
> CURLOPT_VERBOSE option.
>
>
>
> > curl_easy_setopt(d->m_handle, CURLOPT_READFUNCTION, readCallback);
> > curl_easy_setopt(d->m_handle, CURLOPT_READFUNCTION, job);
>
> ^^ I assume this must be a typo, or a copy-paste bug. ^^
>
>
>
> - Jeff

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hi,

I have managed to do some progress on this issue with some modification in
readCallback function.
Here is what contents of the POST that I got through wireshark

File selected: /usr/u.txt
Contents of the file: upload does work!!!!!!!!!!!!!!!!!!

------WebKitFormBoundaryU2idKEu95FuIADhY
Content-Disposition: form-data; name="filecontent"; filename="u.txt"
Content-Type: text/plain

upload does work!!!!!!!!!!!!!!!!!!

------WebKitFormBoundaryU2idKEu95FuIADhY
Content-Disposition: form-data; name="u.x"

27
------WebKitFormBoundaryU2idKEu95FuIADhY
Content-Disposition: form-data; name="u.y"

48
------WebKitFormBoundaryU2idKEu95FuIADhY--

Thus now curl does successfully POST, but response that I get from
rapidshare is "No file selected"
Does anybody know the reason behind this problem?

@Jeff
I tried your solution as well(CURLOPT_HTTPPOST); here is the code which I
used

    int RetVal = 0;
    struct curl_httppost* post = NULL;
    struct curl_httppost* last = NULL;
    FILE *file=NULL;
    FormDataElement fileelement=elements[1];

    file = fopen(fileelement.m_filename.utf8().data(), "rb");
    String mimeType =
MIMETypeRegistry::getMIMETypeForPath(fileelement.m_filename);
    printf("\nmimeType=%s\n",fileelement.m_filename.utf8().data());
    CURL* CurlHandle = curl_easy_init();
    curl_formadd(&post, &last, CURLFORM_COPYNAME,
fileelement.m_filename.utf8().data(),
                  CURLFORM_FILE, file,
                  CURLFORM_CONTENTTYPE, mimeType.utf8().data(),
                  CURLFORM_END);
    curl_easy_setopt(CurlHandle, CURLOPT_POSTFIELDSIZE, size);
    curl_easy_setopt(CurlHandle, CURLOPT_HTTPPOST, post);
    curl_easy_setopt(CurlHandle, CURLOPT_URL, d->m_url);
   RetVal = curl_easy_perform(CurlHandle); //perform the post
   if(RetVal)
    {
        printf("\nerror=%d!!!!\n",RetVal);
    }
    curl_formfree(post); //free memory for form
    curl_easy_cleanup(CurlHandle); //free curl session
    curl_easy_setopt(d->m_handle, CURLOPT_POST, TRUE);

Curl returns error with error code 26(There was a problem reading a local
file or an error returned by the read callback.)
the file which I am selecting is /usr/u.txt

Thanks,
Regards,
Amit
Received on 2008-10-06