wpf - Upload jpeg encoded bitmap to server with httpwebrequest -
i've been trying upload image (jpeg formatted) server. i've used different approaches, none of them worked.
approach 1
i've tried saving jpeg data directly httpwebrequest stream:
//create bitmap. bitmapimage^ bm = gcnew bitmapimage(gcnew uri(path, urikind::relative)); /* stuff bitmap. */ //create jpeg. jpegbitmapencoder enc; enc.frames->add(bitmapframe::create(bm)); //prepare web request. httpwebrequest^ request = dynamic_cast<httpwebrequest^>(webrequest::create(l"http://localhost")); request->contenttype = "image/jpeg"; request->method = "put"; //prepare web request content. stream^ s = request->getrequeststream(); enc.save(s);//throws 'system.notsupportedexception'. s->close(); writing httpwebrequest stream doesn't work, when tested filestream, perfect image created.
approach 2
i tried saving jpeg data memorystream , copying httpwebrequest stream:
//create bitmap. bitmapimage^ bm = gcnew bitmapimage(gcnew uri(path, urikind::relative)); /* stuff bitmap. */ //create jpeg. memorystream^ ms = gcnew memorystream; jpegbitmapencoder enc; enc.frames->add(bitmapframe::create(bm)); enc.save(ms); //prepare web request. httpwebrequest^ request = dynamic_cast<httpwebrequest^>(webrequest::create(l"http://localhost")); request->contenttype = "image/jpeg"; request->method = "put"; //prepare 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 bytes. s->write(buffer, 0, read); s->close(); ms->close(); can tell me i'm doing wrong or give me alternative?
thank you.
insert before while loop:
ms->seek(0, seekorigin.begin); the problem starting read end of stream... doh!
Comments
Post a Comment