NetTalk Central

Author Topic: Examples of file upload/download in a webservice method  (Read 4332 times)

Thys

  • Sr. Member
  • ****
  • Posts: 311
    • View Profile
    • Incasu
    • Email
Examples of file upload/download in a webservice method
« on: January 18, 2017, 09:13:36 PM »
Hi,

I would like to set up to web methods to do a file upload and file download. I have seen the example apps for file uploads/downloads, but I can't find sample code to do this from a webservice method. Any help maybe?

Thanks
Thys

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11244
    • View Profile
Re: Examples of file upload/download in a webservice method
« Reply #1 on: January 19, 2017, 02:38:50 AM »
On the server side it's pretty trivial.

a) Servers can download files "as is" - you just do a normal GET for a filename, and the server sends it. (That's what servers do.)

b) If you wanted to make a _method_ for downloading, set a return field type as stringtheory then in the ServiceMethod routine lost load the file into the object, and base64 encode it.

str.LoadFile('whatever')
str.EncodeBase64()

c) To make a method that uploads a file, set the parameter type to FILE (as distinct from TABLE).
the file will be in a StringTheory object, already base64 decoded, and you can do whatever you like (presumably save it to disk) again in the SeriveMethod routine.

cheers
Bruce

Thys

  • Sr. Member
  • ****
  • Posts: 311
    • View Profile
    • Incasu
    • Email
Re: Examples of file upload/download in a webservice method
« Reply #2 on: January 19, 2017, 08:57:02 PM »
Thanks Bruce.

I got the download working exactly how I wanted it to. But with the upload I have some issues to sort out. I understand that the client will be using a POST call with the file content in the header. Therefore there is an equivalent StringTheory parameter that will receive the content.

Do I then need to use p_web.SaveFile in the ServiceMethod to save the file?
What is the purpose of the first parameter of the SaveFile method? Does it have any significance in this context?
Will the default location be the uploads folder?

Thanks
Thys

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11244
    • View Profile
Re: Examples of file upload/download in a webservice method
« Reply #3 on: January 19, 2017, 11:54:36 PM »
Hi Thys,

The Server supports parameters in various ways;
a) Form-encoded (ie as if they've come from a browser)
b) XML or JSON

If the request is form-encoded then the File is binary, and is parsed into the StringTheory object as binary.

If the request uses XML or JSON, it is assumed the file is base64 encoded, so it's put into the StringTheory object and converted back to binary for you.

The NetTalk client supports all 3 of these, so you have a few options.
Are you making the client in Clarion with NetTalk?
If so form encoding is the easiest.

  client.setvalue('filexx','c:\temp\a.pdf',true)
  client.setvalue('filename','a.pdf')
  client.post(url,'')


>> I understand that the client will be using a POST call with the file content in the header.

the file content is not really in the header - the file content is in the "Post Data" - encoded as either form-encoded, cml, or json.

>> Therefore there is an equivalent StringTheory parameter that will receive the content.

yes, the content is moved into a StringTheory object for you (and converted to binary if necessary.)

>> Do I then need to use p_web.SaveFile in the ServiceMethod to save the file?

no, you'd use the StringTheory.SaveFile method.
p_web.SaveFile is for a different place.

>> Will the default location be the uploads folder?

no. You are expected to pass a "full path name" - if you only pass a simple filename then it will go to the "current directory" whereever that may be set to (and that's not ideal, so set a full pathname.)
If you want it to go to the uploadsfolder (*) then you can use
p_web.site.uploadspath

(*) Do you want it to go to uploads? Files in uploads can be downloaded directly by a browser, not necessarily using your API, so may not be what you want to do.

Cheers
Bruce









Thys

  • Sr. Member
  • ****
  • Posts: 311
    • View Profile
    • Incasu
    • Email
Re: Examples of file upload/download in a webservice method
« Reply #4 on: January 22, 2017, 11:47:30 PM »
Thanks Bruce. It was the easiest thing to do!