Container.ReadFile
------------------

Reads a file with the given path.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [Container](component_Container.md) | [6.4](newinversion64.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "Container.ReadFile"; FilePath { ; Mode; FileName } ) 

**MBS**( **"Container.ReadFile";** /\* Reads a file with the given path. \*/  
**$FilePath**; /\* The native file path.   
Something like "/Users/cs/desktop/test.txt" on Mac and "C:\\Programs\\Data\\test.txt" on Windows. Files without path end in the root directory on Mac.e.g. "test.txt" \*/   
**$Mode**; /\* Optional; If missing or "auto", the plugin will try to detect type by file name extension.  
If filter is "container", the result is returned as a container with a FILE stream inside.  
The type can be specific here like PDF, JPEG, TIFF, GIF, BMP or PSD to return as image container. Or "compressed" to return a compressed container.e.g. "Auto" \*/   
**$FileName**) /\* Optional; If result is container, this defines the file name to use.e.g. "test.txt" \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| FilePath | The native file path.    Something like "/Users/cs/desktop/test.txt" on Mac and "C:\\Programs\\Data\\test.txt" on Windows. Files without path end in the root directory on Mac. | "test.txt" |  |
| Mode | If missing or "auto", the plugin will try to detect type by file name extension.   If filter is "container", the result is returned as a container with a FILE stream inside.   The type can be specific here like PDF, JPEG, TIFF, GIF, BMP or PSD to return as image container. Or "compressed" to return a compressed container. | "Auto" | Optional |
| FileName | If result is container, this defines the file name to use. | "test.txt" | Optional |

### Result

Returns container value or error.

### Description

Reads a file with the given path.  
Reads the data in the file and returns as container.   
See also [Text.ReadTextFile](TextReadTextFile.md) for reading text files with specific encoding.  
  
For reading simple text files, please use [Text.ReadTextFile](TextReadTextFile.md).  
See also [Files.ReadFile](FilesReadFile.md), [Files.ReadJPEG](FilesReadJPEG.md), [Files.ReadPDF](FilesReadPDF.md), [Files.ReadPNG](FilesReadPNG.md), [Text.ReadTextFile](TextReadTextFile.md) and [RichText.ReadFile](RichTextReadFile.md).  
  
This function is optimized for FM 14 and newer to stream file content when using auto mode. This allows to import up to 4 GB of data in small chunks.  
  
FileMaker may decide to create a preview when the plugin passes back the data to FileMaker. If FileMaker on macOS creates a preview for a PDF may take some time and leak memory.  
This function reads the file data into memory and does not define how FileMaker stores it. This is defined by which field it is later assigned to. And the field options define whether this container value is stored internally or externally.  
  
This function requires a native path. Use [Path.FileMakerPathToNativePath](PathFileMakerPathToNativePath.md) to convert a FileMaker path to a native path if required. If you like to have the user choose the path, you can use [FileDialog](component_FileDialog.md) functions.  
For Server be aware that server has limited permissions and may not be able to access all files on a computer.  
### Examples

Read image file:

 Set Variable \[ $fileContent ; Value: MBS ( "Container.ReadFile"; "/Users/cs/Pictures/test.jpg"; "auto")\]  
Read image file with error checking:

 # get native path   
Set Variable \[ $path ; Value: "/Users/cs/Pictures/mond.jpg" \]   
\#   
\# read file   
Set Variable \[ $content ; Value: MBS ( "Container.ReadFile"; $path ) \]   
\#   
\# check for error   
If \[ MBS ("IsError") = 0 \]   
 Set Field \[ Contacts::Photo Container ; $content \]   
Else  
 Show Custom Dialog \[ "Failed to read file." ; $content \]   
End If  
Save as PDF and import it to a field:

 # go to layout and record before   
  
\# build a path for FileMaker   
Set Variable \[ $name ; Value: "test.pdf" \]   
Set Variable \[ $path ; Value: Get(DocumentsPath) &amp; $name \]   
Set Variable \[ $NativePath ; Value: MBS ( "[Path.FileMakerPathToNativePath](PathFileMakerPathToNativePath.md)"; $path ) \]   
\#   
\# Let FileMaker save records   
Save Records as PDF \[ With dialog: Off ; “$path ” ; Records being browsed ; Create folders: Off \]   
Set Variable \[ $error ; Value: Get(LastError) \]   
If \[ $error = 0 \]   
 # Read result PDF  
 Set Variable \[ $PDF ; Value: MBS ( "Container.ReadFile"; $NativePath ) \]   
 If \[ MBS ("ISError") = 0 \]   
 # Put in container  
 Set Field \[ Contacts::PDF File ; $PDF \]   
 Else  
 Show Custom Dialog \[ "Failed to read PDF document" ; $PDF \]   
 End If  
Else  
 Show Custom Dialog \[ "Failed to create PDF document" ; Get(LastErrorDetail) \]   
End If  
Import file on Server on Windows:

 MBS ( "Container.ReadFile"; "C:\\Programs\\FileMaker Server\\Data\\Documents\\test.pdf")  
Import all files in a folder:

 # list files in a folder   
Set Variable \[ $r ; Value: MBS ("[Files.List](FilesList.md)"; $FolderPath ) \]   
  
\# loop over the files   
Set Variable \[ $count ; Value: ValueCount ( $r ) \]   
Set Variable \[ $index ; Value: 0 \]   
If \[ $count &gt;0 \]   
 Loop  
 # build path  
 Set Variable \[$fileName ; GetValue ( $r ; $index +1 )\]  
 Set Variable \[$filePath ; Value: MBS ( "[Path.AddPathComponent](PathAddPathComponent.md)"; $FolderPath ; $fileName )\]  
 Set Variable \[$fileData ; Value: MBS ("Container.ReadFile"; $filePath )\]  
  
 # create record for new file  
 New Record/Request  
 Set Field \[MyData::Container ; $fileData \]  
 Set Field \[MyData::Name ; $fileName \]   
 Commit Records/Requests \[ With dialog: Off \]   
  
 Set Variable \[ $index ; Value: $index + 1 \]   
 Exit Loop If \[ $index = $count \]   
 End Loop  
End If  
### See also

- [Container.GetSize](ContainerGetSize.md)
- [Container.ReadArchive](ContainerReadArchive.md)
- [Container.WriteFile](ContainerWriteFile.md)
- [Path.AddPathComponent](PathAddPathComponent.md)
- [Path.FileMakerPathToNativePath](PathFileMakerPathToNativePath.md)
- [PhotoPicker.File](PhotoPickerFile.md)
- [QLPreviewPanel.SavedFiles](QLPreviewPanelSavedFiles.md)
- [WIA.Image](WIAImage.md)
- [WIA.ImageDialog](WIAImageDialog.md)
- [WinPhotoAcquire.Files](WinPhotoAcquireFiles.md)

### Release notes

- **Version 15.2**
    - Improved functions like [Container.ReadFile](https://www.mbsplugins.eu/ContainerReadFile.shtml) to return empty result if file size is zero.
- **Version 12.0**
    - Fixed [Container.ReadFile](https://www.mbsplugins.eu/ContainerReadFile.shtml) to read empty files.
- **Version 11.3**
    - Fixed a memory leak in [Container.ReadFile](https://www.mbsplugins.eu/ContainerReadFile.shtml) function when used on server and FileMaker creates a PDF preview.

### Example Databases

- [DynaPDF/Extract and analyze images](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/DynaPDF/Extract%20and%20analyze%20images.shtml#3ScriptAnchor_)
- [iOS/Examples/Markup](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/iOS/Examples/Markup.shtml#2ScriptAnchor_)
- [Mac and iOS/Contacts/Contacts](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Mac%20and%20iOS/Contacts/Contacts.shtml#7ScriptAnchor_)
- [Mac and iOS/PDFKit/PDFKit Print Documents](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Mac%20and%20iOS/PDFKit/PDFKit%20Print%20Documents.shtml#4ScriptAnchor_)
- [Mac only/ImageCapture/ImageCapture](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Mac%20only/ImageCapture/ImageCapture.shtml#6ScriptAnchor_)
- [SQL in FileMaker/ImageGallery](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20in%20FileMaker/ImageGallery.shtml#1ScriptAnchor_)
- [Utility functions/Trigger Script on Plugin Error](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Utility%20functions/Trigger%20Script%20on%20Plugin%20Error.shtml#1Button10.0000000395.000000041.0000000512.0000000LayoutObjectAnchor_)
- [WebViewer/WebViewer Save PDF](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/WebViewer/WebViewer%20Save%20PDF.shtml#3ScriptAnchor_)
- [Win Only/PhotoAcquire](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Win%20Only/PhotoAcquire.shtml#7ScriptAnchor_)
- [Win Only/Scanning/WIA Scan](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Win%20Only/Scanning/WIA%20Scan.shtml#9ScriptAnchor_)

### Blog Entries

- [Drag and drop in combination with overlays](https://www.mbsplugins.de/archive/2024-08-05/Drag_and_drop_in_combination_w/monkeybreadsoftware_blog_filemaker)
- [PhotoPicker for iOS with FileMaker iOS SDK](https://www.mbsplugins.de/archive/2022-11-09/PhotoPicker_for_iOS_with_FileM/monkeybreadsoftware_blog_filemaker)
- [Container](https://www.mbsplugins.de/archive/2022-10-20/Container/monkeybreadsoftware_blog_filemaker)
- [Convert Office Files in FileMaker](https://www.mbsplugins.de/archive/2022-07-04/Convert_Office_Files_in_FileMa/monkeybreadsoftware_blog_filemaker)
- [Windows Photo Acquire for FileMaker](https://www.mbsplugins.de/archive/2022-02-07/Windows_Photo_Acquire_for_File/monkeybreadsoftware_blog_filemaker)
- [Combined Components with MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2021-03-29/Combined_Components_with_MBS_F/monkeybreadsoftware_blog_filemaker)
- [Save PDF to container in FileMaker](https://www.mbsplugins.de/archive/2020-11-27/Save_PDF_to_container_in_FileM/monkeybreadsoftware_blog_filemaker)
- [Read HEIC or HEIF image files in FileMaker](https://www.mbsplugins.de/archive/2019-08-20/Read_HEIC_or_HEIF_image_files_/monkeybreadsoftware_blog_filemaker)
- [FileMaker 18 File Script Steps vs. BinaryFile functions](https://www.mbsplugins.de/archive/2019-06-03/FileMaker_18_File_Script_Steps/monkeybreadsoftware_blog_filemaker)
- [Comparing Base Elements Plugin to MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2017-10-24/Comparing_Base_Elements_Plugin/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 2/2025, Seite 24](https://filemaker-magazin.de/neuigkeit/4286-Appetithappen-FMM_202502)
- [Ausgabe 2/2025, Seite 18 bis 19](https://filemaker-magazin.de/neuigkeit/4286-Appetithappen-FMM_202502)
- [Ausgabe 6/2024, Seite 37](https://filemaker-magazin.de/neuigkeit/4277-Appetithappen-FMM_202406)
- [Ausgabe 6/2020, Seite 32](https://filemaker-magazin.de/neuigkeit/4118-Appetithappen-FMM_202006)
- [Ausgabe 4/2020, Seite 33](https://filemaker-magazin.de/neuigkeit/4103-Appetithappen-FMM_202004)

This function is free to use.

Created 6th September 2016 , last changed 27th December 2025

  
[Container.ReadArchive](ContainerReadArchive.md) - [Container.ReadImage](ContainerReadImage.md)

[HTML Version](ContainerReadFile.shtml)