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

Questions, tips and tricks and techniques for scripting in Servoy

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

Postby wes.s.grayson » Thu Mar 08, 2012 6:32 am

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.
wes.s.grayson
 
Posts: 46
Joined: Mon Jan 23, 2012 5:39 pm

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

Postby lwjwillemsen » Thu Mar 08, 2012 8:53 am

HI,

You must supply a field and not a string. You can do it with : foundset[strigger]
Lambert Willemsen
Vision Development BV
lwjwillemsen
 
Posts: 680
Joined: Sat Mar 14, 2009 5:39 pm
Location: The Netherlands

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

Postby wes.s.grayson » Thu Mar 08, 2012 8:42 pm

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
wes.s.grayson
 
Posts: 46
Joined: Mon Jan 23, 2012 5:39 pm

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

Postby lwjwillemsen » Thu Mar 08, 2012 9:59 pm

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,
Lambert Willemsen
Vision Development BV
lwjwillemsen
 
Posts: 680
Joined: Sat Mar 14, 2009 5:39 pm
Location: The Netherlands

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

Postby wes.s.grayson » Fri Mar 09, 2012 12:50 am

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).
wes.s.grayson
 
Posts: 46
Joined: Mon Jan 23, 2012 5:39 pm

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

Postby lwjwillemsen » Fri Mar 09, 2012 9:00 am

Is the value of event.getFormName() null ? See forum id 17203 ? (Servoy bug ?)
Can you test it with 5.2.12 ?
Lambert Willemsen
Vision Development BV
lwjwillemsen
 
Posts: 680
Joined: Sat Mar 14, 2009 5:39 pm
Location: The Netherlands


Return to Methods

Who is online

Users browsing this forum: No registered users and 3 guests