0

I've been trying to upload an image (jpeg formatted) to the server. I've used some different approaches, but none of them worked.

APPROACH 1

I've tried saving the jpeg data directly to HttpWebRequest stream:

//Create bitmap.
BitmapImage^ bm = gcnew BitmapImage(gcnew Uri(PATH, UriKind::Relative));

/*
    Do stuff with bitmap.
*/

//Create the jpeg.
JpegBitmapEncoder enc;
enc.Frames->Add(BitmapFrame::Create(bm));

//Prepare the web request.
HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create(L"http://localhost"));
request->ContentType = "image/jpeg";
request->Method = "PUT";

//Prepare the web request content.
Stream^ s = request->GetRequestStream();
enc.Save(s);//Throws 'System.NotSupportedException'.
s->Close();

Writing to the HttpWebRequest stream doesn't work, but when I tested it with FileStream, a perfect image was created.

APPROACH 2

I also tried saving the jpeg data to a MemoryStream and than copying it to the HttpWebRequest stream:

//Create bitmap.
BitmapImage^ bm = gcnew BitmapImage(gcnew Uri(PATH, UriKind::Relative));

/*
    Do stuff with bitmap.
*/

//Create the jpeg.
MemoryStream^ ms = gcnew MemoryStream;
JpegBitmapEncoder enc;
enc.Frames->Add(BitmapFrame::Create(bm));
enc.Save(ms);

//Prepare the web request.
HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create(L"http://localhost"));
request->ContentType = "image/jpeg";
request->Method = "PUT";

//Prepare the web request content.
Stream^ s = request->GetRequestStream();
int read;
array<Byte>^ buffer = gcnew array<Byte>(10000);
while((read = ms->Read(buffer, 0, buffer->Length)) > 0)//Doesn't read any bytes.
    s->Write(buffer, 0, read);

s->Close();
ms->Close();

Can someone tell me what I'm doing wrong or give me an alternative?

Thank you.

JMRC
  • 1,473
  • 1
  • 17
  • 36
  • What if you use [Stream.CopyTo](http://msdn.microsoft.com/en-us/library/dd782932.aspx) in your second approach, like `ms.CopyTo(s);`? – Clemens Apr 14 '14 at 18:43
  • And what kind of server is listening at `http://localhost`? – Clemens Apr 14 '14 at 18:44
  • @Clemens: .Net 3.0 does not have the Stream.CopyTo function. – JMRC Apr 14 '14 at 18:48
  • @Clemens: It's just a test `index.php` that display the content of the request and the request method. If I assign a binary array to `ms` instead of saving JPEG encoded data, the request and response do work properly, so I don't think that there's a problem with the link. – JMRC Apr 14 '14 at 18:52
  • You may also try to set `request->ContentLength = ms->Length;`. – Clemens Apr 14 '14 at 19:36
  • @Clemens: forgot to reinsert that line, but it doesn't make any difference. – JMRC Apr 14 '14 at 19:44

1 Answers1

1

Insert this before your while loop:

ms->Seek(0, SeekOrigin.Begin);

The problem is you are starting your read from the end of the stream... doh!

J.H.
  • 4,232
  • 1
  • 18
  • 16
  • Yes, that wasn't very smart. I forgot to insert `ms->Position = 0;`, but this time it threw a `ProtocolViolationException`. Now I'm trying to figure out how I can solve this. – JMRC Apr 14 '14 at 21:22