With Windows Client going extinct and Web Client becoming the only way to use Business Central, there are new limitations. One of those limitations is File Management. Even if you have your database on the local machine or owned servers, you still might run into the problem of uploading files to Dynamics 365 Business Central. In this article, we will talk about a few different ways to upload files to the database of Dynamics 365 Business Central.
Cloud Friendly Approach
First, let’s cover the Cloud friendly approach. If you want to move to the Cloud, you will notice that Codeunit “File Management” is missing. Well, it’s not missing, it’s just inaccessible. Event “File” functionality does not work, so the only way to manage files is to use functions “UploadIntoStream” and “DownloadFromStream”. The code to upload file to Dynamics 365 Business Central might look something like this:
This code uploads file to Dynamics 365 Business Central and stores it in a blob field in PlaceHolder Table. Download code might look like this:
Unfortunately, you have to create a name and extension for the file in the code itself, that is because you, essentially, downloading a file from a browser, so the file must be predefined before it gets to the browser. This method can upload files up to 10MB.
If you are not planning to publish your extension to Cloud servers, you can go to your extensions app.json and set your “Target” as “OnPrem” or “Internal”. This way will unlock extra functionality. One of the unlocked functionalities is access to the “File Management” codeunit. It opens other different ways of uploading files to Dynamics 365 Business Central.
Upload files with „File Management“
„File Management“ has at least three ways of uploading files to Dynamics 365 Business Central. They are the same and different at the same time, they all achieve the same goal but in a bit different ways. Let’s start with ‘File.UploadIntoStream‘. This function first uploads the file into the stream, and then you can decide what to do with it.
This example reads the file into the stream and immediately uploads it to the PlaceHolder table. Next, we finally start to use „File Management“.
This time we use the Blob field and one of its functions – uploading straight to the Blob field. As you can see, the function is quite short.
Essentially, this function uploads file to the database service folder and returns a link to the file in a form of text. You can use this link to upload files in several ways. In this example, the file is uploaded straight into the blob field. There are other ways to get this path, for example, ‘FileManagement. UploadFile’.
The last method that we will talk about is ‘FileManagement.BLOBImport’:
As you can see, this is the longest method, but it achieves the same goal.
These different upload methods can allow you to modify or interact with the file before storing it long term. Unfortunately, with downloading files, we have one big limitation: it has to be downloaded from the web browser. That means you will only be able to put a file in one place (Download folder), and you will have to predefine the file before downloading it. One way to do it is to take essential data (such as file type) about a file while uploading it. You can get the file path and just take the extension, name, and store it somewhere near the file itself. When downloading it, do not forget to apply this information.
What I find compelling is that ‘FileManagement.BlobExport’ is the only real way to download files:
The other way, for example, is using “File.Download” or “File.DownloadFromStream”. At first glance, you can select the folder where you want to store your files :
Unfortunately, since the database doesn‘t have access to your file system, it only downloads a file with standard name and extension:
So, in the end, the only two ways to download files from the database are functions „FileManagement.BlobExport“ and „DownloadFromStream”.