POCO : How to upload image to webser using poco in c++
Asked Answered
S

2

8

I am trying to upload an image to remote web server. I have used HTMLForm and FilePartSource. I am able to successfully upload image to local sever (i.e. loclhost) but when i try to upload it in remote server, the response received from remote web server is "411 Length Required ". I tried to set request.setContentLength(sizeofimagefile) but still same issue. Can anyone guide me on what is the issue or . Here is my code.

    HTMLForm htmlform;
htmlform.set("aaaaaa", "bbbbbbb");
htmlform.set("cccccc", "ddddddd");
htmlform.setEncoding(HTMLForm::ENCODING_MULTIPART);

PartSource * pFileSrc = new FilePartSource("filename", "application/octet-stream");

std::istream& mystream = pFileSrc->stream();
mystream.seekg(0, std::ios::end);
int uiLength = mystream.tellg();

    htmlform.addPart("file", pFileSrc);

URI uri("yyy");

    HTTPClientSession session(uri.getHost(), uri.getPort());        
HTTPRequest post_req(Poco::Net::HTTPRequest::HTTP_POST,"xxx",HTTPMessage::HTTP_1_1);        
post_req.setKeepAlive(true);
htmlform.prepareSubmit(post_req);


std::ostream& oustr = session.sendRequest(post_req);
htmlform.write(oustr);

HTTPResponse res;
std::istream& rs = session.receiveResponse(res);

std::cerr << rs.rdbuf(); 

Thanks in advance

Spiffing answered 20/12, 2012 at 8:39 Comment(1)
in setContentLength(sizeofimagefile), Did you include the size of params "aaaaaa" and "cccccc" that you send with your image in the Request dataBlock? If you use POST method for your form, these params goes to the same datablock that the image you are trying to upload.Orphanage
H
5
std::ostream& oustr = session.sendRequest(post_req);
htmlform.write(oustr);

Your code is not able to assign form data into the request object. So when you call session.sendRequest, an empty request is sent to the server. To do a proper conversion of HTMLForm to HTTPRequest, you must write like this -

htmlform.write(session.sendRequest(post_req));

The image upload code which is working for me is -

    HTTPRequest request(HTTPRequest::HTTP_POST, "/fileupload/upload_file.php",    HTTPMessage::HTTP_1_1);
    HTMLForm form;
    form.setEncoding(HTMLForm::ENCODING_MULTIPART);
    form.set("entry1", "value1");
    form.set("entry2", "value2");
    form.addPart("file", new FilePartSource("/home/abc/Pictures/sample.png"));
    form.prepareSubmit(request);

    HTTPClientSession *httpSession = new HTTPClientSession("localhost");
    httpSession->setTimeout(Poco::Timespan(20, 0));
    form.write(httpSession->sendRequest(request));        

    Poco::Net::HTTPResponse res;
    std::istream &is = httpSession->receiveResponse(res);
    Poco::StreamCopier::copyStream(is, std::cout);

The corresponding upload server is using standard PHP code for uploading HTML form files.

Hyper answered 6/2, 2014 at 11:43 Comment(0)
O
0

If you can upload a file to your local server but can't with your remote server, first you should check if your remote Apache/PHP has an upload limit. Try a phpinfo() in your remote server.

http://www.cyberciti.biz/faq/linux-unix-apache-increase-php-upload-limit/

If not, you should revise your code...

From Poco documentation, at URL: http://www.appinf.com/docs/poco/Poco.Net.HTMLForm.html

HTMLForm:

HTMLForm( const HTTPRequest & request, std::istream & requestBody);

Creates a HTMLForm from the given HTTP request. Uploaded files are silently discarded.

And with this constructor:

HTMLForm:

HTMLForm( const HTTPRequest & request, std::istream & requestBody, PartHandler & handler);

Creates a HTMLForm from the given HTTP request. Uploaded files are passed to the given PartHandler.

In your example, What constructor are you applying?

On the other hand,

addPart:

void addPart( const std::string & name, PartSource * pSource ); Adds an part/attachment (file upload) to the form. The form takes ownership of the PartSource and deletes it when it is no longer needed. The part will only be sent if the encoding set for the form is "multipart/form-data"

Try to use "multipart/form-data" with addPart and the second constructor for HTMLForm.

If it doesn't work, try to use a network sniffer like Wireshark to check what are you sendding.

Check if Content-Length header of your Request, have the sizeof(your image) + sizeof("aaaaaa" and "cccccc" params). Or try to send your form with GET method instead of POST.

Let me know if it works.

Regards

Orphanage answered 16/1, 2013 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.