Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 21 total
Thread Upload Image and Receive Image
Mon, Mar 6 2017 6:43 PMPermanent Link

KimHJ

Comca Systems, Inc

I have been searching the post to find anything about uploading a local image to the web server and here is what i came up with except I get an error in the Module when I try to write to the TMemoryStream.

In the EWB I have the following code:

procedure TFrmMain.Uploadtoserver(ImageFile, ImageName: String);
begin
    with ImageRequest do
         begin
              RequestHeaders.clear;
              url := 'modules/phoneapp';
              RequestHeaders.add('Content-Type: application/x-www-form-urlencoded');
              RequestContent.Clear;
             //Here I have login and password //
              RequestContent.Values['imageData'] := ImageFile;
              RequestContent.Values['imageFilename'] := ImageName;
              Execute;  
          
         end;
end;

In the module I have this code:

procedure TEWBModule1.EWBModuleExecute(Request: TEWBServerRequest);
var
TempImage, TempImageName: String;

if (Request.RequestMethod=rmPost) then
       begin
              TempContent:=TStringList.Create;
              TempImage:=TempContent.Values['imageData'];
              TempImageName:=TempContent.Values['imageFilename'];
              SaveImage(TempImage,TempImageName);
     end;

procedure TEWBModule1.SaveImage(ImageData, ImageName);
var
ImageStream: TMemoryStream;
begin
       ImageStream := TMemoryStream.Create;
       ImageStream.Write(ImageData, length(ImageData)); // here I get access violation in the module //
      
       ImageStream.SaveToFile('C:\MyImages\' + ImagesName);       
       ImageStream.Free;
end;  

Thanks for any help.
Kim
Mon, Mar 6 2017 7:41 PMPermanent Link

KimHJ

Comca Systems, Inc

I forgot about the Base64.
Change to this and now it works.

ImageStream.Write(DecodeBase64(ImageFiledata), length(ImageFiledata) * SizeOf(ImageFiledata));

Thanks,
Kim
Tue, Mar 7 2017 5:16 AMPermanent Link

Matthew Jones

KimHJ wrote:

> procedure TEWBModule1.SaveImage(ImageData, ImageName);

Is that as it is? Not sure I know what happens if there are no parameter types.

I tried to find a suitable stream write to compare, but all mine seem to be char buffers.

So, next step I'd take would be some debugging. You need to log what is happening - does EWB have any logging built in? Something like Codesite would be a good way forward, or just write to file. I'd save those SaveImage strings to disk to check their content. And do some validity around ImageData and writing to stream. Strings always seem to trip me up, with me sometimes needing to do the @ImageData[1] trick and sometimes not.

--

Matthew Jones
Tue, Mar 7 2017 5:35 AMPermanent Link

Ronald

"Matthew Jones" wrote:

<So, next step I'd take would be some debugging. You need to log what is happening - does EWB have any logging built in? Something like Codesite would be a good way forward, or just write to file. I'd save those SaveImage strings to disk to check their content. And do some validity around ImageData and writing to stream. Strings always seem to trip me up, with me sometimes needing to do the @ImageData[1] trick and sometimes not.>

I do not create a MemoryStream, but I use this:

TMemoryStream(Request.Files[x].Stream).SaveToFile('c:\temp\afilename);

Ronald
Tue, Mar 7 2017 10:42 AMPermanent Link

KimHJ

Comca Systems, Inc

Ronald wrote:

>>I do not create a MemoryStream, but I use this:

>>TMemoryStream(Request.Files[x].Stream).SaveToFile('c:\temp\afilename);


I did try it and since the image is Base64 encoded I got an error.
This will work:

TMemoryStream(DecodeBase64(Request.Files[x].Stream), length(Request.Files[x].Stream) * SizeOf(Request.Files[x].Stream)).SaveToFile('c:\temp\afilename);

Kim
Tue, Mar 7 2017 2:15 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Kim,

<< I have been searching the post to find anything about uploading a local image to the web server and here is what i came up with except I get an error in the Module when I try to write to the TMemoryStream. >>

When uploading files, you should always use a POST method along with the an HTML form encoding of feMultiPartFormData:

http://www.elevatesoft.com/manual?action=viewprop&id=ewb2&comp=THTMLForm&prop=Encoding

That will ensure that the files are encoded properly as multi-part MIME attachments that the EWB Web Server can decode.  After that, it's a simple matter to grab them in your module via the TEWBServerRequest.MIMEParts property:

http://www.elevatesoft.com/manual?action=viewprop&id=ewb2mod&product=rsdelphiwin32&version=10B&comp=TEWBServerRequest&prop=RequestMIMEParts

They will already be in binary form, completely-decoded and ready to go.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Mar 7 2017 2:19 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Ronald,

<< I do not create a MemoryStream, but I use this:

TMemoryStream(Request.Files[x].Stream).SaveToFile('c:\temp\afilename); >>

Is this supposed to be used with the EWB Web Server/Modules ?  There isn't any Files property for the TEWBServerRequest class.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Mar 7 2017 8:46 PMPermanent Link

KimHJ

Comca Systems, Inc

Tim Young [Elevate Software] wrote:

>>When uploading files, you should always use a POST method along with the an HTML form encoding of feMultiPartFormData:

Now I'm lost. I'm using Andreas Blenk FileUpload I have an image in the TImage I now need to send it to the module using TServerRequest POST.
Where would I add the THTMLFormEncoding?
How would I add the image data so the server can get it with RequestMIMEParts?
When I use the below the RequestMIMEParts.Count is 0.
Can I grab the image from the TImage or would it still be Base64 encoded?

with ImageRequest do
         begin
              RequestHeaders.clear;  
              url := 'modules/papp';
              RequestHeaders.add('Content-Type: multipart/form-data');
              RequestContent.Clear;
              
              RequestContent.Values['imageData']:=ImageFile; (Base64 Image)
              RequestContent.Values['imageFilename']:='myimage';
              Execute;  
         end;

Thanks,
Kim
Wed, Mar 8 2017 6:21 AMPermanent Link

Ronald

Tim Young [Elevate Software] wrote:

<<There isn't any Files property for the TEWBServerRequest class.>>

You are right, sorry, I had webbroker in my mind.
Wed, Mar 8 2017 2:06 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Kim,

<< Now I'm lost. I'm using Andreas Blenk FileUpload I have an image in the TImage I now need to send it to the module using TServerRequest POST.  >>

Okay, I'll need to check out his code to see what he's doing.

For now, this is how to get started:

http://www.elevatesoft.com/supportfaq?action=view&category=ewb&question=tserverrequest_formdata

However, it should be a bit easier if I actually just implement the File API external interface in the WebDOM unit, so I'll see what I can do.  But, for now, this is considered "unsupported" and the "supported" way of doing things is to use a THTMLForm instance with a TFileComboBox instance for specifying the file to upload.

Tim Young
Elevate Software
www.elevatesoft.com
Page 1 of 3Next Page »
Jump to Page:  1 2 3
Image