Avatar of Roger Alcindor
Roger Alcindor

asked on 

How to decode a Jpeg image from a web POST

I am writing a windows Web application using embarcadero XE10.2 Berlin C++ builder;
The application uses a TWebModule and receives a POST web action from a web page being displayed on a remote mobile device.
the mobile device posts a jpeg image which Is received in the following function, st2 is a TMemoryStream.
I am copying the raw content into a TByteDyneArray b so that I can easily inspect it. I load the TMemoryStream with the raw content and then save the memory stream to a file with the extension .jpg.
This file is reported as corrupt or invalid or too long when I try to open it with windows Photo viewer or the like.
how do I extract the image data ?
The size of the raw data as given by  Request->RawContent.Length is
Inspection of the beginning of the raw content  up to encountering a NULL is shown in quotes below.
"------WebKitFormBoundary9lWvgPDQcTZoDcRf\r\nContent-Disposition: form-data; name=\"picture\"; filename=\"15123250544981385201258.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n�￘�£\x02tExif"

void TWebModule1::Action3Post(TWebRequest *Request,TWebResponse *Response)
{
	UnicodeString u,v,uu="";
	TByteDynArray b;
	char buff='a';

	int n,m,w;
	u = Request->ContentType;
	n = Request->RawContent.Length;
	m = Request->Files->Count;
	b = Request->RawContent;
	st2->Clear();
	b.set_length(n);
	st2->WriteData(Request->RawContent,Request->RawContent.Length);
    st2->Position = soBeginning;

//	Form1->Memo1->Lines =  Request->ContentFields;
	for(;;)
	{
		st2->Read(&buff,1);
		uu +=buff;
		if(buff==2)
		{
			for(;;)
			{
				st2->Read(&buff,1);
				uu +=buff;
				if(buff==0)
					break;
			}
			break;
		}
	}
	st2->SaveToFile(u"testpicture.jpg");

Open in new window


The html5 code in the web application that leads to the "post" action is shown below :
<div id="div1">
	<form name="scanner" id="scannerForm" action="" method="post" enctype="multipart/form-data"> 
	<label style ="margin-top:0px; font-size:50px;"for="barcodescan">Scan Barcode</label>
		<input name="picture" type="file" id="barcodescan" accept="image/*;capture=camera">
	</form>
</div>
<script>
    document.getElementById("barcodescan").onchange = function() {
          if( this.files.length != 0 ) {
                 document.getElementById("scannerForm").submit();
          }
          else {
                alert("no barcode picked, please try again!");
          }
    }
</script>

Open in new window

Thanks roger
C++Images and PhotosDelphi

Avatar of undefined
Last Comment
Roger Alcindor

8/22/2022 - Mon