Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id : GU0xJlLJLnyrmmuas8/1MW

How to work with files in AX 7 Part 1 – upload, download, read and store files in DB

Tayfun Sertan Yaman Profile Picture Tayfun Sertan Yaman 280

Ability to work with local files and folders in the new cloud based AX7 (which we used to do with WinAPI class methods) is now depreciated. Instead, we have some new options for working with files on the cloud environment like Azure BLOB storage, temporary storage and Sharepoint. Since the new interface is web based, we have ability to upload files into various new storage options and download those using urls, instead of direct interaction with disks, files and folders.

Now let’s have a look at those new file upload methods starting from a single application which we upload a file to temporary storage and after upload finishes, display its file path and download it back.

class fileInteractionTest
{

public static void main(Args _args)
{
FileUploadTemporaryStorageResult result;

result = File::GetFileFromUser(classStr(FileUploadTemporaryStorageStrategy));</pre>
if(result && result.getUploadStatus())
{
Info(result.getDownloadUrl());
new Browser().navigate(result.getDownloadUrl());
}

}

}

This job will display user a dialog to upload a file and with our chosen upload strategy, will storage it to our new temporary storage option, Azure temporary blob storage.  Methods will return us an url to our uploaded file instead of a temporary filename inside Windows\temp folder :

capture001

Files stored in Azure temporary blob storage are erased on each system restart, disk expansion or whenever Azure wants. So don’t upload any files here unless you want to use them immediately.

The file upload strategy class simply defines the target we want to upload our file into. There are many different options which we can send our uploaded file. If you select FileUploadStrategyBase class in AOT and click view hierarchy you can get an overview of those :

capture002

In this blog we will only use the temporary storage option.

The file upload operation returns us a FileUploadResult class to control the result of our file upload. There are different result classes for different file upload strategies. In our upload strategy, we can query the download url which points to our file in temporary storage. To download it, we call the navigate method of Browser class which redirects us to the download url.

What if we want to edit the file or want to save it to database? For that purpose, AX7 uses .NET stream classes. You can open the file by downloading the file URL into a MemoryStream class and use either the legacy .NET stream methods like ReadByte() and Write(), or .NET StreamReader and StreamWriter classes for easier operation. The openResult() method in our Result class downloads the file from the upload url into a .NET MemoryStream class.

Now let’s create a more advanced example, this time we will use the form file upload control on a form that will get an XML file from the user and place it into the database. Later in another blog we will use another form to read and download those XML files.

We create our upload form like this and set the desired upload strategy for our form upload control :

capture003

And create a simple table which saves the uploaded xml document per date :

capture004

To get our uploaded file result, we override the OnUploadCompleted() method our form upload control :

[Form]
public class fileUploadForm extends FormRun
{
//Tip : Do not use URL EDT type for blob storage URLs, these can be longer than URL datatype!
str fileurl;

[Control("Custom")]
class FileUpload1
{
public void OnUploadCompleted()
{
FileUploadTemporaryStorageResult result = this.getFileUploadResult() as FileUploadTemporaryStorageResult;

super();

if(result && result.getUploadStatus())
{
fileurl = result.getDownloadUrl();

using(System.IO.MemoryStream stream = result.openResult() as System.IO.MemoryStream)
using(System.IO.StreamReader sreader = new System.IO.StreamReader(stream, System.Text.Encoding::UTF8, true))
{
XML filexml;
TestXMLTable testXml;

filexml = sreader.ReadToEnd();

info(filexml);

if(filexml)
{
ttsbegin;
testXml.clear();
testxml.uploaddate = DateTimeUtil::utcNow();
testXml.xml = filexml;
testXml.insert();
ttscommit;
}
}
}
}
}

[Control("Button")]
class DownloadImmediately
{
public void clicked()
{
super();
new Browser().navigate(fileurl);
}
}
}

capture005

Note that we are using the new AX7 ‘using’ keyword working with the .NET streams and stream readers in order to dispose them immediately after they are out of scope.
By clicking the download immediately button, we can directly download our file from the temporary storage url returned by the upload result.

In the next blog, we are going to create another example form to browse the XML files from DB using different methods and attaching them as files into the records with new AX7 document management.

You can download the test project used in this blog from GitHub link below:

TestFileInteraction.axpp

 

[adinserter block=”9″]


This was originally posted here.

Comments

*This post is locked for comments