SmartCard.ReadFile
------------------

Read file from smart card with APDU protocol.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [SmartCard](component_SmartCard.md) | [8.5](newinversion85.md) | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes, on macOS and Windows | ❌ No |

MBS( "SmartCard.ReadFile"; SmartCardContext; FileID; ResultType ) 

**MBS**( **"SmartCard.ReadFile";** /\* Read file from smart card with APDU protocol. \*/  
**$SmartCardContext**; /\* The smartcard context reference number. \*/   
**$FileID**; /\* The fileID of the file to retrieve.  
Given as hex string.e.g. "2F07" \*/   
**$ResultType**) /\* How to return data.  
Can be JPEG, Hex or Base64.e.g. "Hex" \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| SmartCardContext | The smartcard context reference number. | $SmartCardContext |
| FileID | The fileID of the file to retrieve.   Given as hex string. | "2F07" |
| ResultType | How to return data.   Can be JPEG, Hex or Base64. | "Hex" |

### Result

Returns data or error.

### Description

Read file from smart card with APDU protocol.  
Returns data in desired encoding or error message.  
The first two bytes (4 Hex characters) are status code.  
For JPEG we remove status code to return only image.  
For Swiss health cards, the relevant IDs are 2F06 and 2F07.  
  
For Belgian EID:  
  
| 4031 | ID RN |
|--:|---|
| 4032 | SGN RN |
| 4033 | ID Address |
| 4034 | SGN Address |
| 4035 | ID Photo |

  
You find the IDs for files usually in documentation or your card!  
For some cards you may first need to move down a hierarchy of folders to get an item via [SmartCard.Transmit](SmartCardTransmit.md).  
### Examples

Read file from smartcard with APDU protocol:

 Set Variable \[ $result ; Value: MBS ( "SmartCard.ReadFile"; $$SmartCard ; Card::FileID ) \]   
If \[ MBS ("IsError") \]   
 Set Field \[ Card::Status ; $result \]   
 Show Custom Dialog \[ "Error" ; $result \]   
Else  
 Set Field \[ Card::FileData ; $result \]   
 Set Field \[ Card::Status ; "OK " &amp; Left ( $result ; 4 ) \]   
 #   
 Perform Script \[ Specified: From list ; “Split” ; Parameter: \]  
End If  
Query name from Swiss health card:

 # Initialize smart card:   
Set Variable \[ $$SmartCard ; Value: MBS ( "[SmartCard.Init](SmartCardInit.md)" ) \]   
If \[ MBS ("IsError") \]   
 Show Custom Dialog \[ "Failed to initialize SmartCard" ; $$SmartCard \]   
 Exit Script \[ Text Result: \]   
End If  
\#   
\# Connect to a reader   
Set Variable \[ $reader ; Value: "Identiv SCR3310 uTrust 2700 R" \]   
Set Variable \[ $r ; Value: MBS ( "[SmartCard.Connect](SmartCardConnect.md)"; $$SmartCard ; $Reader ; "Shared"; "any" ) \]   
If \[ MBS ("IsError") \]   
 Show Custom Dialog \[ "Connect failed" ; $r \]   
 Exit Script \[ Text Result: \]   
End If  
\#   
\# Read file from smartcard with APDU protocol.   
\#   
\# Read record with name   
Set Variable \[ $result ; Value: MBS ( "SmartCard.ReadFile"; $$SmartCard ; "2F06") \]   
If \[ MBS ("IsError") \]   
 Set Field \[ Card::Status ; $result \]   
 Show Custom Dialog \[ "Error" ; $result \]   
 Exit Script \[ Text Result: \]   
End If  
\# Pick result   
Set Variable \[ $filedata ; Value: $result \]   
\#   
\# Split in values   
Set Variable \[ $json ; Value: MBS ("[SmartCard.SplitValues](SmartCardSplitValues.md)"; $FileData ) \]   
\# Get item 128, which is full name   
Set Variable \[ $name ; Value: JSONGetElement ( $json ; "128" ) \]   
Set Variable \[ $NameList ; Value: Substitute($name ; ", "; ¶) \]   
Set Variable \[ $LastName ; Value: GetValue($nameList ; 1) \]   
Set Variable \[ $FirstName ; Value: GetValue($nameList ; 2) \]   
\#   
Show Custom Dialog \[ "Name" ; $FirstName &amp; " " &amp; $LastName \]   
Set Variable \[ $r ; Value: MBS ( "[SmartCard.Disconnect](SmartCardDisconnect.md)"; $$SmartCard ; "Eject") \]  
### See also

- [IsError](IsError.md)
- [SmartCard.Connect](SmartCardConnect.md)
- [SmartCard.Disconnect](SmartCardDisconnect.md)
- [SmartCard.Init](SmartCardInit.md)
- [SmartCard.Release](SmartCardRelease.md)
- [SmartCard.SplitValues](SmartCardSplitValues.md)
- [SmartCard.Transmit](SmartCardTransmit.md)

### Release notes

- **Version 10.2**
    - Fixed problem in [SmartCard.ReadFile](https://www.mbsplugins.eu/SmartCardReadFile.shtml) with MacOS Catalina.
- **Version 10.0**
    - Fixed issue from 9.5 where [SmartCard.ReadFile](https://www.mbsplugins.eu/SmartCardReadFile.shtml) would not read last chunk correctly.
    - Improved code for [SmartCard.ReadFile](https://www.mbsplugins.eu/SmartCardReadFile.shtml) to read in chunks of 256 bytes instead of 255 bytes.
- **Version 9.5**
    - Improved code for [SmartCard.ReadFile](http://www.mbsplugins.eu/SmartCardReadFile.shtml) to work for more cards.
- **Version 8.5**
    - Added [SmartCard.ReadFile](http://www.mbsplugins.eu/SmartCardReadFile.shtml) and [SmartCard.SplitValues](http://www.mbsplugins.eu/SmartCardSplitValues.shtml) functions to read data from various smartcards (e.g. Swiss Health Insurance Card or Belgian Identity Card).

### Example Databases

- [SmartCard/Belgian EID Reader](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SmartCard/Belgian%20EID%20Reader.shtml#2ScriptAnchor_)
- [SmartCard/SmartCard ReadFile](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SmartCard/SmartCard%20ReadFile.shtml#10ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 10.2pr2](https://www.mbsplugins.de/archive/2020-04-06/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Neues MBS FileMaker Plugin 10.0](https://www.mbsplugins.de/archive/2020-01-14/Neues_MBS_FileMaker_Plugin_100/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 10.0 - More than 6000 Functions In One Plugin](https://www.mbsplugins.de/archive/2020-01-14/MBS_FileMaker_Plugin_100_-_Mor/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 9.6pr1](https://www.mbsplugins.de/archive/2019-12-02/MBS_FileMaker_Plugin_version_9/monkeybreadsoftware_blog_filemaker)
- [Neues MBS FileMaker Plugin 9.5](https://www.mbsplugins.de/archive/2019-11-12/Neues_MBS_FileMaker_Plugin_95/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 9.5 - More than 5900 Functions In One Plugin](https://www.mbsplugins.de/archive/2019-11-12/MBS_FileMaker_Plugin_95_-_More/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 9.5pr6](https://www.mbsplugins.de/archive/2019-11-04/MBS_FileMaker_Plugin_version_9/monkeybreadsoftware_blog_filemaker)
- [Read patient name from Swiss Health Card](https://www.mbsplugins.de/archive/2019-03-08/Read_patient_name_from_Swiss_H/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 8.5pr3](https://www.mbsplugins.de/archive/2018-10-24/MBS_FileMaker_Plugin_version_8/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 19th October 2018 , last changed 6th December 2019

  
[SmartCard.ReadCertificate](SmartCardReadCertificate.md) - [SmartCard.Reconnect](SmartCardReconnect.md)

[HTML Version](SmartCardReadFile.shtml)