使用wxHTTP Post请求上载wxImage

Upload wxImage with wxHTTP Post request

本文关键字:上载 wxImage 请求 Post wxHTTP 使用      更新时间:2023-10-16

如何通过wxHTTP POST请求上传存储在wxImage或wxBitmap中的图像?我知道我可以用

wxImage::SaveFile (wxOutputStream &stream, wxBitmapType type) and
wxHTTP::SetPostBuffer (const wxString &contentType, const wxMemoryBuffer &data)

但我刚开始用cpp和wx。

您就快到了。缺少的部分是实现wxOutputStream接口的某个类,然后可以使用SetPostBuffer()方法发送其内容。

您可以在这里看到wxOutputStream的所有实现。你好像在找wxMemoryOutputStream

因此,完整的代码部分应该是这样的:

wxMemoryOutputStream stream;
if (!myImage.SaveFile(stream, wxBITMAP_TYPE_PNG))
    ;// TODO: Handle error
SetPostBuffer("image/png", *stream.GetOutputStreamBuffer());

我就是这么做的。有点详细,但减去错误检查。。。成员变量是wxURL。


  // ok now read the data into a buffer again
  wxMemoryBuffer buf;
  {
    // don't have charBuf around for too long
    wxFile f(someFileName);
    char charBuf[f.Length()];
    f.Read(charBuf, f.Length());
    buf.AppendData(charBuf, f.Length());
  }
  // upload!
  wxHTTP post; 
  post.SetPostBuffer("application/gzip", buf);
  post.Connect(mDestUrl.GetServer()):
  wxInputStream *iStream = post.GetInputStream(mDestUrl.GetPath() + "?" + mDestUrl.GetQuery());
  // put result into stream
  wxString res;
  wxStringOutputStream oStream(&res);
  iStream->Read(oStream);
  wxLogDebug(TAG " server replied: " + res);

请注意,wxHttp不进行多部分上传。例如,在PHP中,您可以通过访问数据

$postdata = file_get_contents("php://input");