NetTalk Central

Author Topic: Base64 file encoding on client  (Read 2763 times)

Thys

  • Sr. Member
  • ****
  • Posts: 311
    • View Profile
    • Incasu
    • Email
Base64 file encoding on client
« on: February 02, 2017, 10:13:11 PM »
Hi,

I've managed to do a file upload to a NT web service using client-side JavaScript, for which I'm very thankful - it solves a huge problem for us.

But now with a Clarion client doing the file upload, I don't know how to base64 encode the file content before it is uploaded. In the server side's ServiceMethod routine, I use the following code:

   FileContent.Base64Decode ()
   FileContent.SaveFile (clip (p_web.site.UploadsPath) & '\' & DocumentID)

In the Clarion code, where I think the file must be encoded first, I use this code:

   nt.SetValue ('FileContent', GLO:FileName, true)
   nt.SetValue ('FileExtension', GLO:Extension)
   nt.Post (GLO:URL, '')

There is a method for nt.Base64Encode, but it takes a string as parameter - which supposedly is the file content to be encoded. If I introduce a StringTheory variable to do the encoding, how do I adjust the content provided by the SetValue statement for FileContent then before the POST is done?

Thanks
Thys

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11186
    • View Profile
Re: Base64 file encoding on client
« Reply #1 on: February 05, 2017, 09:58:40 PM »
Hi Thys,

>> But now with a Clarion client doing the file upload, I don't know how to base64 encode the file content before it is uploaded. In the server side's ServiceMethod routine, I use the following code:

You don't need to base64 encode the upload.
Let's back up a bit.

There are (at least) 3 ways for a client to send data to the server.
a) Form Encoded
b) as an XML payload
c) as Json payload.

when you do this;

   nt.SetValue ('FileContent', GLO:FileName, true)
   nt.SetValue ('FileExtension', GLO:Extension)
   nt.Post (GLO:URL, '')


All the work is done for you, and the payload is in "form encoded" mode.

On the server side, you set the parameter to FILE.
This places the incoming file into a StringTheory object, already in Binary form. If you want to save it to disk you can do;

FileContent.SaveFile (clip (p_web.site.UploadsPath) & '\' & DocumentID)

There is no need to base64 decode it, since it was never base64 encoded in the first place.

IF you are uploading XML or JSON (which you are not doing, nor do you need to do it) then the field would be base64 encoded, but if you were constructing XML or JSON there are then techniques to do that. If that was the case the server would _still_ not need to do a Base64 Decode - that would be done automaticlly for you, and the code to save the file would remain as

FileContent.SaveFile (clip (p_web.site.UploadsPath) & '\' & DocumentID)

cheers
Bruce




Thys

  • Sr. Member
  • ****
  • Posts: 311
    • View Profile
    • Incasu
    • Email
Re: Base64 file encoding on client
« Reply #2 on: February 06, 2017, 03:15:53 AM »
Thanks Bruce.

I'm trying to match the upload from a page with how a Clarion client would do the upload. I assumed (wrongly) that the client needs to base64 encode the file content before sending it. I did manage to send base64 encoded content to the server - I just had to decode it before saving the file.

I now to find another way to encode the file content as part of the XMLHTTPRequest.send () method's parameter in the script.

Thanks
Thys

Thys

  • Sr. Member
  • ****
  • Posts: 311
    • View Profile
    • Incasu
    • Email
Re: Base64 file encoding on client
« Reply #3 on: February 06, 2017, 03:41:45 AM »
Bruce,

Just to clarify why file uploads by both page scripts and Clarion apps are important - the file upload service should be available for any client. So originally I had the Clarion client working, but not the page scripts. Then when I base64 encoded the file content on the page scripts and decoded it on the server, the situation has swapped around now with Clarion saving a corrupted file.

Thys

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11186
    • View Profile
Re: Base64 file encoding on client
« Reply #4 on: February 07, 2017, 12:14:55 AM »
It's likely easiest to make the Clarion client do "anything". So I recommend getting the JavaScript one working, then set the Clarion one to match that.

cheers
Bruce