Jump to content

Exporting Container Field to File


This topic is 2397 days old. Please don't post here. Open a new topic instead.

Recommended Posts

As  you can't use the Export Field Contents step when running a script through FileMaker Server, I threw together the following ScriptMaster script. Any suggestions or improvements welcome. 

 

Write Container to File ( filePath ; containerField )

import org.apache.commons.io.*;

InputStream input;
FileOutputStream out;

try{
	input = fmpro.getContainerStream(containerField);
}
catch(e){
 return false;
}

if ( input == null ) {
 return false;
}

try{
	output= new FileOutputStream( filePath );
}
catch(e){
 return false;
}

IOUtils.copy (input, output );

//int read = 0;
//byte[] bytes = new byte[1024];
//while ((read = input.read(bytes)) != -1) {
// output.write(bytes, 0, read);
//}

return true;
Link to comment
Share on other sites

  • 9 months later...

Hi Jonathan,

 

Thank you posting this. I think it's exactly what I'm looking for, but I'm having trouble implementing.

 

The function returns 0, and the containers fail to "export".

 

I'm trying to write two container fields to a server's temporary folder.

 

 

The variable that defines the folder:

 $PATHfolder = /private/var/folders/g6/3h4y328x1q5gc59q4hr_4xjr0000gn/T/tempKMZ_9A1902/

The variables that define the file paths:

$PATHxml = /private/var/folders/g6/3h4y328x1q5gc59q4hr_4xjr0000gn/T/tempKMZ_9A1902/sample.xml
$PATHpng = /private/var/folders/g6/3h4y328x1q5gc59q4hr_4xjr0000gn/T/tempKMZ_9A1902/19-tag.png

Does that look right?

 

Or could it be an error in my ScriptMaster module? I pasted your code, downloaded Apache Commons IO, and added five jars:

commons-io-2.4-javadoc.jar

commons-io-2.4-sources.jar
commons-io-2.4-test-sources.jar
commons-io-2.4-tests.jar
commons-io-2.4.jar
 
Not sure how to attach a screenshot, but it would show five Commons-Io selected in the Jars tab of the "Edit Script" layout.
 
Thanks again for the module. Any further help is greatly appreciated.
=L=
_____
Leo Di Croce
 
 
Feb 19, 2014 - Found a solution here.
Link to comment
Share on other sites

  • 2 weeks later...

Hey there,

 

1. The only jar you should need is "Commons-Io-2.4.jar". You shouldn't need any of the other jars.

2. There are two input variables.

containerField - is the NAME of the container field. It should NOT be the value of container field itself.

filePath - is the POSIX file path destination for the file in the container field.

 

 

Hope that helps,

JP

Link to comment
Share on other sites

Here's the sample file. The call to the function is:

WriteContainertoFile( "/Users/Shared/" & WriteContainerField::ContainerName ; "WriteContainerField::ContainerField" )

Also attached is a screenshot of the script in ScriptMaster and the JAR config in ScriptMaster. Its a pretty simple script to implement.

WriteContainerField.fmp12.zip

post-73209-0-33428600-1394313179_thumb.p

post-73209-0-53960400-1394313189_thumb.p

Link to comment
Share on other sites

  • 10 months later...

Jonathan

 

simplified code...

import org.apache.commons.io.*
 
if ( !input ) {
	return false
}

try{
	input = fmpro.getContainerStream(containerField)
	output= new FileOutputStream( filePath )
	IOUtils.copy (input, output )
}
catch(e){
	return false
}
 
return true
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Just tested it and am getting the same issue as you...

It's stopping at about 299k

 

Tried it with about 3 or 4 other methods, but still the same result, indicating choking on the input side...

 

And then i tried it with other PDF files (of which I have many for test, up to 50MB)

And it all works as expected.

I suspect that source PDF is in some way not lovely on the inside...

 

Opened it with Acrobat Pro, re-saved it. All lovely. It's the PDF not the code.

 

 My code however is bad..

 

try

import org.apache.commons.io.IOUtils

try{
	input = fmpro.getContainerStream(containerField)
	if ( !input ) {
		return false
	}
	output= new FileOutputStream( filePath )
	IOUtils.copy(input, output)
        //for files over 2GB
        //IOUtils.copyLarge(input, output)
}
catch(e){
	return false
	//return e
}
 
return true
Link to comment
Share on other sites

Thank you so much. Can I ask about the large file option…I am a geneticist and have VERY large files sometimes. Is there some "cost" to implementing the Large file option all the time?

 

Also, I have an issue with the java code reading a file into a container with large files…is there a similar solution for that? And also reading the content of a large file! Sorry to bother you guys but I'm a scientist not great programmer and these 3 things will make my life complete!!  :yep:

Link to comment
Share on other sites

add another parameter which indicates if the size is large..

 

for the other issues you might need to be using Buffered Stream methods if the files are large. posting code will help us to help you here..

import org.apache.commons.io.IOUtils

try{
	input = fmpro.getContainerStream(containerField)
	if ( !input ) {
		return false
	} //end if
	output= new FileOutputStream( filePath )
	if (sizeParam){
		//for files over 2GB
		IOUtils.copyLarge(input, output)
	} else {
		IOUtils.copy(input, output)
	} //end if
}
catch(e){
	//turn on to see what errors are
	//return e
	return false
} //end try
 
return true
Link to comment
Share on other sites

It's the standard "Read File Contents" module in Scriptmaster:

 

if( pathToFile == null ) throw new Exception("You must supply a pathToFile parameter");

return new InputStreamReader( new FileInputStream( pathToFile ), "utf-8" );

 

And the standard "Get File As Container" module:

 

File f = new File( pathToFile );

fileExists = f.exists();

return f;

 

I have run out of memory with both I believe with large files

Link to comment
Share on other sites

  • 4 months later...
  • Newbies

Have you try with Mail-it ?

 

Emai_ExportFile( fileObject; filePath {; saveMode; protectFile } )

/*
Exports file object from container field to disk [i.e. an attachment that has
been retrieved from a message and stored in FileMaker]
fileObject - File object to be exported
filePath - Path [including file name and extension] to which the file will
be saved. If path does not include file name the default file name
from object will be used.
saveMode - "strict" [default], "rename" or "overwrite"
protectFile - Used to write-protect the exported [temp] file if it should only
be used for viewing purposes by the user. "modify" [default] does
not write-protect the file. "read-only" protects it from modification.
saveMode="strict" this function fails if there is an existing file.
If saveMode="rename" plug-in adds a counter to the file name if
there is a conflict with an existing file. If saveMode="overwrite"
plug-in tries to overwrite existing file with the same name [if any].

*/

Link to comment
Share on other sites

  • 1 year later...
  • Newbies

The free BaseElements plug-in has a similar function:

BE_ExportFieldContents ( field ; outputPath )

Exports the contents of the container field to disk at the outputPath specified. Container fields should be inserted via "Insert File". This provides a similar functionality to the Export Field Contents script step, but is also available via FileMaker Server.

 
Link to comment
Share on other sites

  • 1 month later...
  • 4 months later...

Here's another ScriptMaster module which doesn't require Apache IOUtils and supports FileMaker-style paths. It uses some internal ScriptMaster utilities instead, I'll add it to the ScriptMaster list of modules. Returns number of bytes transferred. The 4096 is the buffer size. 

Parameters:

  • containerName
  • path

Script:

import com.prosc.io.IOUtils;
import com.prosc.fmkit.PluginUtils;

inputStream = fmpro.getContainerStream(containerName)
if (inputStream == null) throw new IOException(containerName + " is empty");

try {
	File f = PluginUtils.fileForFMPath(path);
	FileOutputStream out = new FileOutputStream(f);
	IOUtils.writeInputToOutput(inputStream, out, 4096);
} finally {
	out.close();
	inputStream.close();
}

 

 

Link to comment
Share on other sites

  • 3 weeks later...

Hi shmert,

On 17. August 2017 at 8:25 PM, shmert said:

Here's another ScriptMaster module which doesn't require Apache IOUtils and supports FileMaker-style paths. It uses some internal ScriptMaster utilities instead, I'll add it to the ScriptMaster list of modules. Returns number of bytes transferred. The 4096 is the buffer size. 

can you explain me, which extensions or java files I have to install and where to put them? If I try your script, it returns just "error".

Regards,

Thorsten

Link to comment
Share on other sites

This topic is 2397 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.