FileUploadingAPI Usage

Hi,

I want to go to a URL in an AbstractEditor class and fetch a PDF (full of Entity values). I want to be able to save this PDF file as a FileDescriptor against the Entity but not sure how to use the FileUploadingAPI methods. Obviously that is an interface and FileUploading class which implements that interface doesn’t seem to be a class you can just instantiate and use?

Thanks

Hi Mark,

It’s a bean, so you can inject it to a field:

public class EmployeeEdit extends AbstractEditor<Employee> {

    @Inject
    private FileUploadingAPI fileUploadingAPI;

See full example here: https://doc.cuba-platform.com/manual-6.6/images_recipe.html

Hi Mark,

your description is a bit short, so I make some assumption for my answer.

Assumtion:

  1. You have in your entity a field of type FileDescriptor which will store the PDF.
  2. You have a field in your GUI to enter the URL (even as a entity field or just a field which is not stored)
  3. You want to download your PDF directly to the FileStorage
  4. You want to start the download automatically when you close (save) the editor (if not you need some button to initiate this action)
  5. You use a HTTP GET for your URL

So I think you shouldn’t use the FileUplaodingAPI, I guess the FileLoader class is a better choice.

Let me describe to process in general:
You need to implement the preCommit() method in your AbstractEditor class (see Documentation) which checks if URL is entered and establish a HTTP connection to download the PDF from your URL as an InputStream.
HTTP connection can be generated as easiest by standard JAVA (but you can also use some other lib, e.g apache httpclient):

InputStream response = null;
HttpURLConnection httpURLconnection = new URL(url).openConnection();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
	response = connection.getInputStream();
}

Now with the FileLoader method
FileLoader.saveStream(response)
you will create a FileDescriptor and assign that to your entity field.

Please keep in mind that I didn’t add enough error-handling in the sample, this is up to you.
Please see the referenced documentation, it includes good examples…
The FileLoader class should be injected (as Konstantin mention for the FileUplaodingAPI)

Please ask if you need more details.

CU
Steven

1 Like