Jonathan Perel Posted April 19, 2013 Posted April 19, 2013 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;
Jonathan Perel Posted April 23, 2013 Author Posted April 23, 2013 Oh... you'll need Apache.org's Common-IO library for this to work. http://commons.apache.org/proper/commons-io/
Leo Di Croce Posted February 18, 2014 Posted February 18, 2014 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.
Jonathan Perel Posted March 4, 2014 Author Posted March 4, 2014 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
Ibrahim Bittar Posted March 8, 2014 Posted March 8, 2014 Hi Jonathan Can you post a sample file. I'm having a hart time trying to make it work. Sorry, I'm new to Java and Groovy. Regards Ibrahim
Jonathan Perel Posted March 8, 2014 Author Posted March 8, 2014 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
john renfrew Posted February 2, 2015 Posted February 2, 2015 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 1
Bailey Kessing Posted February 3, 2015 Posted February 3, 2015 Hmmmm. When I run this I get a PDF file that is not complete or damaged. See the two PDFs below. About Downloads (Original).pdf About Downloads (Writen from Container).pdf
Bailey Kessing Posted February 13, 2015 Posted February 13, 2015 I am having problems with this…any one else?
john renfrew Posted February 13, 2015 Posted February 13, 2015 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
Bailey Kessing Posted February 14, 2015 Posted February 14, 2015 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!!
john renfrew Posted February 14, 2015 Posted February 14, 2015 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
Bailey Kessing Posted February 19, 2015 Posted February 19, 2015 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
Bailey Kessing Posted February 19, 2015 Posted February 19, 2015 Also, your code from Posted 13 February 2015 - 03:03 PM works but your latest one doesn't. I guess you didn't finish it with the size parameter part?
Newbies TheRoth Posted July 8, 2015 Newbies Posted July 8, 2015 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 hasbeen retrieved from a message and stored in FileMaker]fileObject - File object to be exportedfilePath - Path [including file name and extension] to which the file willbe saved. If path does not include file name the default file namefrom object will be used.saveMode - "strict" [default], "rename" or "overwrite"protectFile - Used to write-protect the exported [temp] file if it should onlybe used for viewing purposes by the user. "modify" [default] doesnot 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 ifthere is a conflict with an existing file. If saveMode="overwrite"plug-in tries to overwrite existing file with the same name [if any]. */
Newbies martinspanjaard Posted February 22, 2017 Newbies Posted February 22, 2017 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.
jrr316 Posted March 28, 2017 Posted March 28, 2017 I am playing around with this, but found that it is not compatible with the Get(DocumentsPath) or Get (TemporaryPath) commands in Filemaker. Anyone have any luck with that?
bcooney Posted March 29, 2017 Posted March 29, 2017 I would guess that BE expects OS paths, not FM paths. https://www.briandunning.com/cf/902
john renfrew Posted March 29, 2017 Posted March 29, 2017 https://baseelementsplugin.zendesk.com/hc/en-us/articles/205350417-Notes-about-File-Paths <<The paths used by the plugin are Operating System paths, not FileMaker paths.......>> and same is true for ScriptMaster functions.
shmert Posted August 17, 2017 Posted August 17, 2017 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(); }
Thorsten Kaltenborn Posted September 4, 2017 Posted September 4, 2017 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
Recommended Posts
This topic is 2728 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 accountSign in
Already have an account? Sign in here.
Sign In Now