Overview
Features
Download
Documentation
Community
Add-Ons & Services

Simple Multipart upload client

A general discussion forum.

Simple Multipart upload client

Postby srjones » 17 Mar 2010, 15:00

Hi,

I've recently downloaded the Poco libraries to implement a simple file upload functionality, however I'm encountering an issue where the Content-Length of the message does not get set, causing the upload to fail. Here is what I have currently:

Code: Select all
   try 
   {
      URI uri("http://server:80/fileupload");
      std::string path(uri.getPathAndQuery());

      HTTPClientSession session(uri.getHost(), uri.getPort());
      HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
      HTTPResponse res;
      req.setHost( uri.getHost(), uri.getPort());
      req.set("User-Agent", "POCO 1.3.6p2");
      req.setKeepAlive(true);

      HTMLForm form(Poco::Net::HTMLForm::ENCODING_MULTIPART);
      FilePartSource file1("c:\\test.txt");
      FilePartSource file2("c:\\test2.txt");

      form.addPart("test.txt", & file1);
      form.addPart("test2.txt", & file2);

      form.prepareSubmit(req);
      form.write(session.sendRequest(req));

      session.receiveResponse(res);

      std::cout << res.getStatus() << ": " << res.getReason() << std::endl;
   }
   catch ( Exception& ex )
   {
      std::cerr << ex.displayText() << std::endl;
      return 1;
   }


I've tried using a CountingOutputStream to determine the form size, then set the Content-Length, however this empties the FileStreams, so that the outgoing multipart message has no file content. I'm sure I'm missing something simple to determine the Form size, however I just don't see it. Any help would be greatly appreciated.

thanks,
Steve
srjones
 
Posts: 2
Joined: 17 Mar 2010, 14:48

Re: Simple Multipart upload client

Postby guenter » 18 Mar 2010, 07:17

Use chunked transfer encoding.
Call
Code: Select all
req.setChunkedTransferEncoding(true);

before sending the form.
guenter
 
Posts: 1107
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Simple Multipart upload client

Postby srjones » 23 Mar 2010, 19:18

Hi guenter,

Thanks for your reply. Sorry it took so long for me to get back to you, but I was still having problems getting the client to upload to my server, which was running the Apache Commons fileUpload library. After trying a few different things, I found that the content-disposition tag was causing the problem. When I ran the client using the code I posted previously, each part of the upload message had a content-disposition of "file". After looking through RFC 1867, from my understanding all top-level parts should have a content-disposition of "form-data", then subparts can have a content-disposition of "file". However, since it seems to be fine that each file can be a part in the message with the "form-data" content-disposition, I made a small change in HTMLForm.cpp. In there I changed line 356 from

std::string disp("file; name=\"");

to

std::string disp("form-data; name=\"");

Making this change fixed my problem, and the code is uploading to the server fine now.

Thanks for your help,
Steve
srjones
 
Posts: 2
Joined: 17 Mar 2010, 14:48

Re: Simple Multipart upload client

Postby guenter » 21 Apr 2010, 07:11

guenter
 
Posts: 1107
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest