Open/Save file from web client

I am developing a web client based solution and would like the ability to save/export a file from the servoy window in the browser (like downloading a file from a website).

The files will be csv and pdf format, generated from scripts.

Can this be done? If so, could someone give me some pointers?

Thanks

Dexadrine:
I am developing a web client based solution and would like the ability to save/export a file from the servoy window in the browser (like downloading a file from a website).

The files will be csv and pdf format, generated from scripts.

Can this be done? If so, could someone give me some pointers?

Thanks

Isn’t it default media field behaviour?

Riccardino:
Isn’t it default media field behaviour?

Care to elaborate?

In the smart client, to export a csv i would write a method to open the save file dialog (so that the user can choose the location) and write the text file to that location, fairly simple. I am slightly baffled with how to recreate this type of thing in the web client.

Dexadrine:

Riccardino:
Isn’t it default media field behaviour?

Care to elaborate?

In the smart client, to export a csv i would write a method to open the save file dialog (so that the user can choose the location) and write the text file to that location, fairly simple. I am slightly baffled with how to recreate this type of thing in the web client.

If you store your file in a media field, then you can let the user download it in his chosen location.

OK here’s what I have so far:

Script 1:
I create a dataset and do the following:

//create a csv from the dataset
var csv = dataset.getAsText(',','\n','"',true);

//create the temp csv and store it in the media field for downloading
var tempFile = plugins.file.createTempFile('report','.csv');
var success = plugins.file.writeTXTFile(tempFile,csv);
if(success) {globals.g_csvfile = tempFile;}

I have a button with the following:
Script2:

application.showURL(globals.g_csvfile,'_blank');

This works in smart client (opens the csv file), but not in web client, it opens the “install smart client” page.

What am I doing wrong?

Remember - the WEB client - you’re in a browser.

Browser = “dumb” application that draws HTML from a server.

In the Smart Client you’re running Java on the local client.

Java Client = “smart” client that can interact with the local file system, etc.

It works in Smart Client because the file is downloaded to the local machine. Using the Web Client, the “client” is running on the SERVER and not the local machine, so the file isn’t downloaded to the local machine, therefore it can’t be opened.

Hope this helps.

Dexadrine:
I am developing a web client based solution and would like the ability to save/export a file from the servoy window in the browser (like downloading a file from a website).

The files will be csv and pdf format, generated from scripts.

Can this be done? If so, could someone give me some pointers?

Thanks

When a PDF opens within a browser you can click the disk icon that is located in the upper lefter corner of the screen (just above the pdf) to download and save.

With the csv I believe you can write it to the built-in Tomcat web server ROOT folder or subfolder and then pass back a link to that file that can be right-clicked (on Windows platform) and saved locally.

Dean

You can use the blobloader and stream files from the database or a global media field to the (web)client.

Paul

Thanks guys,

I cracked it a couple of days ago with this:

//create a csv from the dataset
var csv = dataset.getAsText(',','\n','"',true);

//create the temp csv and store it for downloading
var fileName = 'report.csv';
var tempFile = './server/webapps/ROOT/' + fileName;
var success = plugins.file.writeTXTFile(tempFile,csv);

pbakker:
You can use the blobloader and stream files from the database or a global media field to the (web)client.

Paul

Whats the blobloader?

Make sure that you don’t fill up the server with all the export files users create :-)

Search the forum, I’d say… :D

But with the BlobLoader you can extract files from the DB or globals and use them in the same way as you would use files from Servoys media library inside HTML.

In the latest 3.1 beta’s more functionality has been added, so if you have a global containing a file, you can use that as well, you can set the mime-type and filename as well.

for example, if you create a calculation with the following code, and place a HTML Area on your form with the Calc as DataProvider, then, in the WebClient, clicking the link would open the file.

var URL = 'media:///servoy_blobloader?servername=' + currentcontroller.getServerName() + '&tablename=XXXXX&dataprovider=YYYYY&rowid1=' + ZZZZZ+'&mimetype='+AAAAA+'&filename='+BBBBBB
	return '<html><body><a target="_blank" href="'+URL+'">test</a></body></html>';

Where:

  • XXXXX is the name of the table your file is stored in
  • YYYYYY is the columnname of the column where the file is stored
  • ZZZZZ is value of the PK field of the row (In case of a PK buildup out of multiple columns, add &rowid2=value … &rowidXX=value)
  • AAAAA is the mimetype of the file
  • BBBBB is the name of the file

Regards,

Paul

If you’re using Web Client then you don’t have to do any of that coding at all.

Just use:

controller.printPreview()

On the Smart Client it will go into print preview, when you’re viewing your solution on the web SERVOY will automatically create a PDF for you.

Well, if you’d like to print, that’s the best, but if I want to open a word file, an Image, CSV file or any other file in their native editor, I think this is the way to go.

Paul

Paul, I 100% agree with you. GREAT code sample!

pbakker:
In the latest 3.1 beta’s more functionality has been added, so if you have a global containing a file, you can use that as well, you can set the mime-type and filename as well.

Thank you for pointing this out and for the nice example of this great new feature!

Dean

Can someone see what is wrong with the following line.
I think I do like expected, but for some reason I don’t see the correct image:

This is my code:

if (_to_webshop.button_image_add_to_cart)
	elements.btn_add_to_cart.setImageURL('media:///servoy_blobloader?servername='+ _to_webshop.getServerName()+'&tablename='+ _to_webshop.getTableName()+'&dataprovider=button_image_add_to_cart&webshop_id=' + _to_webshop.webshop_id + '&mimetype=image/png');

The relation _to_webshop points to 1 single record.
When I do _to_webshop.getSize() I get 1 as result

I’ve put the same line as application.output and I see the following:

media:///servoy_blobloader?servername=myServer&tablename=webshop&dataprovider=button_image_add_to_cart&webshop_id=f7fb71a2-b8fb-4b97-9a17-087042dd642b&mimetype=image/png

That looks correct. It is also the correct webshop_id

But the image I see is from another record (first record) in the webshop table.

I also tried to replace ‘webshop_id=’ by ‘rowid1=’, but that doesn’t change anything.

Does someone see in the coding line what is wrong?

I tried all kind of combinations.

The following is working:

elements.btn_add_to_cart.setImageURL('media:///servoy_blobloader?servername='+ _to_webshop.getServerName()+'&tablename='+ _to_webshop.getTableName()+'&dataprovider=button_image_add_to_cart&rowid1=' + _to_webshop.webshop_id + '&mimetype=image/png');

Important is also not to put double quotes around the value (so not rowid1=“f7fb71a2-b8fb-4b97-9a17-087042dd642b”, which I tried before)

Wouldn’t it be more logic to use the real columnname in stead of rowid1?

Unfortunately I was too early happy.

It works perfect in Smart client but not at all in webclient

I made an example solution.
I hope that Support can see what is wrong

Martin

pbakker:
Search the forum, I’d say… :D

But with the BlobLoader you can extract files from the DB or globals and use them in the same way as you would use files from Servoys media library inside HTML.

In the latest 3.1 beta’s more functionality has been added, so if you have a global containing a file, you can use that as well, you can set the mime-type and filename as well.

for example, if you create a calculation with the following code, and place a HTML Area on your form with the Calc as DataProvider, then, in the WebClient, clicking the link would open the file.

var URL = 'media:///servoy_blobloader?servername=' + currentcontroller.getServerName() + '&tablename=XXXXX&dataprovider=YYYYY&rowid1=' + ZZZZZ+'&mimetype='+AAAAA+'&filename='+BBBBBB
return '<html><body><a target="_blank" href="'+URL+'">test</a></body></html>';



Where:
- XXXXX is the name of the table your file is stored in
- YYYYYY is the columnname of the column where the file is stored
- ZZZZZ is value of the PK field of the row (In case of a PK buildup out of multiple columns, add &rowid2=value ... &rowidXX=value)
- AAAAA is the mimetype of the file
- BBBBB is the name of the file

Regards,

Paul

What is the syntax for using with a global media field?
What is the syntax for using with a application.showURL?

The above example worked perfectly, but I am having trouble getting it to work while using with a global and with show URL.

Dean Westover
Choices Software, Inc.