Am Donnerstag, Dezember 09, 2010, 13:43:49 schrieb DEXTER MCCONNELL:
> Hi  got an error when I used this code
> 
> std::ofstream f("result.dat");
>     std::copy(it->getBodyBegin(), it->getBodyEnd(),
>     std::ostreambuf_iterator<char>(f));
> 
> I want to save the uploaded file to disk
> 
> How to I fetch the name of the uploaded file?
> 
> Thank you
> Dexter

Error? What error? It works for me.

And you get the filename using it->getFilename(). But notice, that it returns a 
std::string and the constructor of ofstream needs a const char*. You have to 
do something like:

std::ofstream f(it->getFilename().c_str());

And here is a complete example:

----------------------------upload.ecpp
<%pre>
#include <fstream>
</%pre>
<html>
 <body>
  <form method="post" enctype="multipart/form-data">
   <input type="file" name="upload">
   <input type="submit">
  </form>

<%cpp>

  const tnt::Multipart& mp = request.getMultipart();
  tnt::Multipart::const_iterator it = mp.find("upload");

  if (it != mp.end())
  {
    log_info("save file to upload/" << it->getFilename());
    std::ofstream f(("upload/" + it->getFilename()).c_str());

    std::copy(it->getBodyBegin(),
              it->getBodyEnd(),
              std::ostreambuf_iterator<char>(f));
</%cpp>

    <p>
    <$ it->getSize() $> bytes received<br>
    saved to upload/<$ it->getFilename() $>
    </p>
<%cpp>
  }
</%cpp>
 </body>
</html>
----------------------------

Tommi

------------------------------------------------------------------------------
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to