1. lingaiah T
  2. SnapDevelop
  3. Saturday, 14 November 2020 14:56 PM UTC

First Thank you appeon Team, doing great job...

FOR SNAP DEVELOP API, How to download a file and upload file via api. I tried different ways but i could n't.

If there is a sample project / script it's help full to me.

api can use either PB 2019 or webbrowser.

 

thanks in advance...

 

Accepted Answer
Daryl Foster Accepted Answer Pending Moderation
  1. Monday, 16 November 2020 07:08 AM UTC
  2. SnapDevelop
  3. # Permalink

Hi Lingaiah, as with everything C# there is a heap of different ways to upload and download files from an API.  I've attached a zip file called FileController.zip which contains a single file FileController.cs.  FileController.cs is a really basic example of how to upload and download a file from a SnapDevelop project.  It was created in SnapDevelop 2019 R2.  Just create a sample ASP.NET Core Web API application using the Basic sample code.  If your application is called WebApplication1 then you can just drop the FileController.cs file into the Controllers directory and it should work.  If your application is not called WebApplication1, just update this line in FileController.cs to replace WebApplication1 with the name of your application:

namespace WebApplication1.Controllers

The application will let you POST a file and save it to the attachments directory.  You can then GET the file using the fileName GET argument.

e.g.

POST http://localhost:5000/api/file/upload

GET http://localhost:5000/api/file/download?fileName=testfile.pdf

 

The GET command you can just use from a browser.  The POST command can be done from Postman, but it is probably just as easy to use curl.  Here are the curl commands:

To upload:

curl -F uploadedFile=@testfile.pdf http://localhost:5000/api/file/upload

 

To download:

curl -G localhost:5000/api/file/download?filename=testfile.pdf --output testfile.pdf

This is a really basic example with no real error checking.  The upload function just returns a HTTP 201 for a successful upload, but in real life I would probably have it return JSON with some data about the uploaded file (I would probably give it a unique filename and return that in the response).  I would also probably send more data into the upload function with a multipart form.  There is lots you can do with it, but hopefully this gives you some idea of where to start.

 

Attachments (1)
Comment
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Monday, 2 August 2021 03:28 AM UTC
  2. SnapDevelop
  3. # 1

Hi Thomas,

To upload it from Powerbuilder you just need to code it as a multipart form upload.  There are a few posts on the forum about how to do it, but here is a simple example without any validation.  For this particular example it's a multipart form with a single part.

 

long ll_FileNum
blob blb_file
blob blb_boundary
blob blb_terminus
blob blb_multipart
integer li_rc
HTTPClient lnv_HttpClient

string ls_endpoint
string ls_file
integer li_ResponseStatusCode = 0
string ls_ResponseBody = ''
string ls_ResponseStatusMessage = ''


// Hard code the file name and endpoint for testing
ls_file = 'c:\temp\invoice.pdf'
ls_endpoint = 'http://localhost:5000/api/file/upload'

// Create a boundary marker for the multipart blob
string ls_Boundary = "$$$Boundary$$$"

// Read the file into a blob
ll_FileNum = FileOpen(ls_file, StreamMode!)
if ll_FileNum > 0 then
    FileReadEx(ll_FileNum, blb_file)
    FileClose(ll_FileNum)
else
    MessageBox('File Error',  'Error Opening Filename ' + ls_file)
    return
end if

// Create component blobs
blb_boundary = blob('~r~n--' + ls_Boundary + '~r~n', EncodingUTF8!)
blb_file = blob( 'Content-Disposition: form-data; name="uploadedFile"; filename="invoice.pdf"~r~n' + &
                        "Content-Type: application/pdf~r~n~r~n", EncodingUTF8!) + &
                        blb_file
blb_terminus = blob('~r~n--' + ls_Boundary + '--~r~n', EncodingUTF8!)

// Concatenate blobs into a single multipart blob
blb_multipart = blb_boundary + blb_file + blb_terminus

// Send the request
lnv_HttpClient = create HTTPClient
lnv_httpClient.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + ls_BOUNDARY)

li_rc = lnv_httpClient.SendRequest('POST', ls_endpoint, blb_multipart)

// obtain the response data
if li_rc = 1 then
    li_ResponseStatusCode = lnv_HttpClient.GetResponseStatusCode()
    ls_ResponseStatusMessage = lnv_HttpClient.GetResponseStatusText()
    lnv_HttpClient.GetResponseBody(ls_ResponseBody, EncodingUTF8!)

    if li_ResponseStatusCode = 200 then
        MessageBox('Upload Successfull', ls_ResponseBody)
    else
        MessageBox('Upload Failed', ls_ResponseBody)
    end if
else
    MessageBox('HTTP Error', 'Error calling ' + ls_endpoint + '.  Return Code ' + string(li_rc))
end if

Comment
  1. Thomas Rolseth
  2. Monday, 2 August 2021 16:58 PM UTC
This worked perfectly. I think something was wrong with how I was combining blobs in the other example I was using. I need to load large PDFs into the database via the API (which I can do). In terms of sending them, would it be better to use this file controller or to base64 encode the blob and send that over the wire? I'm not sure encoding really large files will work.



Thanks, Tom
  1. Helpful
  1. Daryl Foster
  2. Tuesday, 3 August 2021 01:16 AM UTC
Hi Thomas, where are you thinking of base64 encoding the blob, from your Powerbuilder client? I tend to try and avoid the need for base64 encoding unless absolutely necessary. It adds processing overhead at both the sending and receiving end and it increases the amount of data that needs to be sent.



Can you give an example of the client server flow for your proposed use of base64?
  1. Helpful
There are no comments made yet.
Thomas Rolseth Accepted Answer Pending Moderation
  1. Saturday, 31 July 2021 17:44 PM UTC
  2. SnapDevelop
  3. # 2

Daryl, 

I've added FileController.cs to my SnapDevelop API and it works when I use it with Swagger.  I'm having a hard time calling this from a PowerBuilder client app however.  I'm using GetFileOpenName() to select a file (PDF) but am not quite sure how to pass it to the controller via restclient or httpclient.  Do you have any sample code that you can share that illustrates how this could be done?

Thanks, Tom 

Comment
  1. Daryl Foster
  2. Monday, 2 August 2021 03:30 AM UTC
Hi Thomas, see my reply above which has some sample code to help you.
  1. Helpful
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Monday, 16 November 2020 04:12 AM UTC
  2. SnapDevelop
  3. # 3

Hi Lingaiah, are you writing the API that clients will call to upload and download files?

e.g, with endpoints (controllers) similar to these:

https://my.webserver.com/api/download

https://my.webserver.com/api/upload

or are you trying to call an API from your C# that will upload and download files?

Comment
  1. lingaiah T
  2. Monday, 16 November 2020 04:42 AM UTC
Yes, I want to create a controller to download/upload files. Is it required to add anything in startup.cs? controller script also i don't know. can you explain?
  1. Helpful
  1. Daryl Foster
  2. Monday, 16 November 2020 07:09 AM UTC
See my reply above
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Saturday, 14 November 2020 16:25 PM UTC
  2. SnapDevelop
  3. # 4

Hi Lingiah;

   SNAP is just plain C#, so you can just use the .Net frameworks FTP API. Here is the download API example. For PB, just create a .Net Assembly with that code. The new PB 2019 R2 can then consume the Assembly directly in this release.

HTH

Regards... Chris

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.