← servoy magazine

[Tip] How to put files in blob/media fields

  • Tips

by Harjo Kompagnie
www.directict.nl

Did you know you can put files in a blob/media field an than open them from within your solution?

Here is the example:

To put files into the blob/media and fill the appropriate fields do this:

controller.newRecord();
filename = application.showFileOpenDialog();
if (filename)//cancel when not available
{
    var normalizedFileName = "";
    normalizedFileName = utils.stringReplace(filename,'\\','/')    //make windows path like unix path
    filepath = normalizedFileName

    var idx = normalizedFileName.lastIndexOf("/");
    if (idx != -1) normalizedFileName = normalizedFileName.substring(idx+1 );
    filenames = normalizedFileName
    media = application.readFile(filename)
    filesize = plugins.file.getFileSize(filename)
}

The next thing you have to do is make a method that opens the files right away from the database:

var a = filenames.lastIndexOf(".");
var b = filenames.substr(0,a);
var c = filenames.substring(a);

var tempfilename = application.createTempFile(b,c);
application.writeFile(tempfilename,media);

if(utils.stringMiddle(application.getOSName(),1,7) == "Windows") //open on Windows
{
    application.executeProgramInBackground('rundll32', 'url.dll,FileProtocolHandler',tempfilename)
}
else
{
    if (utils.stringLeftWords(application.getOSName(), 1) == "Mac") // open on Mac
{
application.executeProgram('open', tempfilename);


Cheers

Harjo Kompagnie

Comments (1)

Marcel Trapman
GREAT STUFF. I used this one, after it was published on the forum, to write my own methods. The only thing missing is Linux support. Who will come up with that part?