Page 1 of 1

global method to view files stored in media field(s)

PostPosted: Thu Mar 08, 2012 6:32 am
by wes.s.grayson
I have about 20 different MEDIA fields in my database where users upload PDF files. I would like to create a method that allows the user to click a button beside any one of those fields and VIEW the PDF file contained in that field. I currently accomplish this by using the code below.

Code: Select all
var field = user_briefing
var fileName = 'c:/temp/servoyFile.pdf'
var success = plugins.file.writeFile(fileName,field);
application.executeProgram('rundll32', 'url.dll,FileProtocolHandler', fileName);


This works great, except if I do it this way, I will have to write 20 different methods, since the field name (user_briefing in this case) is hard coded in. I would rather obtain the desired field name based on what "View" button was clicked. For example, the View button next to the user_briefing field is named btn_user_briefing. I use the following method to get the field name in a variable, then use that variable to hopefully accomplish the same as above.

Code: Select all
//find the name of the button that triggered the action
var trigger = event.getElementName()
//shorten the name to eliminate the first 4 characters
var strigger = trigger.substring(4)

var fileName = 'c:/temp/servoyFile.pdf'
var success = plugins.file.writeFile(fileName,strigger);
application.executeProgram('rundll32', 'url.dll,FileProtocolHandler', fileName);


This doesn't work, the error I get is:
Can't find method com.servoy.extensions.plugins.file.FileProvider.js_writeFile(string,string)

Thoughts? Servoy 5.2.9, Windows, SmartClients.

Re: global method to view files stored in media field(s)

PostPosted: Thu Mar 08, 2012 8:53 am
by lwjwillemsen
HI,

You must supply a field and not a string. You can do it with : foundset[strigger]

Re: global method to view files stored in media field(s)

PostPosted: Thu Mar 08, 2012 8:42 pm
by wes.s.grayson
thanks for the tip. Could you be more specifc about what the coding would look like? I get the error "foundset not defined" when I attempt the following:

Code: Select all
//find the name of the button that triggered the action
var trigger = event.getElementName()
//shorten the name to eliminate the first 4 characters
var strigger = trigger.substring(4)

var fileName = 'c:/temp/servoyFile.pdf'
var success = plugins.file.writeFile(fileName,foundset[strigger]);
application.executeProgram('rundll32', 'url.dll,FileProtocolHandler', fileName);


thanks

Re: global method to view files stored in media field(s)

PostPosted: Thu Mar 08, 2012 9:59 pm
by lwjwillemsen
If the method resides in the same form foundset should be accessible.
* If not you must get the formname from the event to get to a foundset : forms[event.getFormName()].foundset[strigger]

Oh, I know see it's a global method so look at *

Regards,

Re: global method to view files stored in media field(s)

PostPosted: Fri Mar 09, 2012 12:50 am
by wes.s.grayson
since I need this to be a global method, I'll have to obtain the form name through the method. So when I do the following as you suggested:

Code: Select all
var fileName = 'c:/temp/servoyFile.pdf'
var urlPath = fileName
var trigger = event.getElementName()
//shorten the name to eliminate the first 4 characters
var strigger = trigger.substring(4)

var fileName = 'c:/temp/servoyFile.pdf'
var success = plugins.file.writeFile(fileName,forms[event.getFormName()].foundset[strigger])
application.executeProgram('rundll32', 'url.dll,FileProtocolHandler', fileName);


I get the following error:
Can't find method com.servoy.extensions.plugins.file.FileProvider.js_writeFile(string,org.mozilla.javascript.Undefined).

Re: global method to view files stored in media field(s)

PostPosted: Fri Mar 09, 2012 9:00 am
by lwjwillemsen
Is the value of event.getFormName() null ? See forum id 17203 ? (Servoy bug ?)
Can you test it with 5.2.12 ?