Codeunit definition

The codeunit is called SaaSFileMgt and contains two procedures:

  • UploadFile: This function will handle the file upload to Azure Blob Storage.
  • DownloadFile: This function will handle the file download from Azure Blob Storage.

In the codeunit, we have two global variables, both of which contain the URLs of the Azure functions to call:

var
BaseUrlUploadFunction: Label 'https://saasfilemgt.azurewebsites.net/api/UploadFile?code=YOURFUNCTIONKEY';
BaseUrlDownloadFunction: Label
'https://saasfilemgt.azurewebsites.net/api/DownloadFile?code=YOURFUNCTIONKEY';

Here, YOURFUNCTIONKEY is the key we use to access the Azure function (retrieved from the Azure portal by selecting the function and clicking on Manage).

The UploadFile procedure is defined as follows:

    procedure UploadFile()
var
fileMgt: Codeunit "File Management";
selectedFile: Text;
httpClient: HttpClient;
httpContent: HttpContent;
jsonBody: text;
httpResponse: HttpResponseMessage;
httpHeader: HttpHeaders;
fileName: Text;
fileExt: Text;
base64Convert: Codeunit "Base64 Convert";
instr: InStream;
begin
UploadIntoStream('Select a file to upload','','',selectedFile,instr);
fileName := delchr(fileMgt.GetFileName(selectedFile), '=', '.' +
fileMgt.GetExtension(selectedFile));
fileExt := fileMgt.GetExtension(selectedFile);
jsonBody := ' {"base64":"' + tempblob.ToBase64String() +
'","fileName":"' + fileName + '.' + fileExt +
'","fileType":"' + GetMimeType(selectedFile) + '", "fileExt":"' +
fileMgt.GetExtension(selectedFile) + '"}';
httpContent.WriteFrom(jsonBody);
httpContent.GetHeaders(httpHeader);
httpHeader.Remove('Content-Type');
httpHeader.Add('Content-Type', 'application/json');
httpClient.Post(BaseUrlUploadFunction, httpContent, httpResponse);
//Here we should read the response to retrieve the URI
message('File uploaded.');
end;

From the preceding code, we can see the following:

  1. We ask for a file to upload.
  2. We read the file into a Stream object.
  3. We retrieve some parameters related to the file (name and extension).
  4. We create a JSON message, as requested by the function (as we described previously).
  5. Then, we send an HTTP POST request to our Azure function (by using the HttpClient object), passing the JSON in the body.

The DownloadFile procedure is defined as follows:

    procedure DownloadFile(fileName: Text; blobUrl: Text)
var
tempblob: Codeunit "Temp Blob";
httpClient: HttpClient;
httpContent: HttpContent;
jsonBody: text;
httpResponse: HttpResponseMessage;
httpHeader: HttpHeaders;
base64: Text;
fileType: Text;
fileStream: InStream;
base64Convert: Codeunit "Base64 Convert";
outstr: OutStream;
begin
fileType := GetMimeType(fileName);
jsonBody := ' {"url":"' + blobUrl + '","fileName":"' + fileName + '", "fileType":"' +
fileType + '"}';
httpContent.WriteFrom(jsonBody);
httpContent.GetHeaders(httpHeader);
httpHeader.Remove('Content-Type');
httpHeader.Add('Content-Type', 'application/json');
httpClient.Post(BaseUrlDownloadFunction, httpContent, httpResponse);
httpResponse.Content.ReadAs(base64);
base64 := DelChr(base64, '=', '"');
base64Convert.FromBase64(base64);
tempblob.CreateOutStream(outstr);
outstr.WriteText(base64);
tempblob.CreateInStream(fileStream);
DownloadFromStream(fileStream, 'Download file from Azure Storage', '', '', fileName);
end;

This procedure receives the name of the file to retrieve as input. This is how it works:

  1. It calls a custom function (GetMimeType) that returns the content type of this file, and then we compose the JSON message for the request.
  2. Then, we send an HTTP POST request to our Azure function (by using the HttpClient object), passing the JSON in the body.
  3. We read the HttpResponse object (Base64 of the retrieved file) and download it to the client-side by using an InStream object and calling the DownloadFromStream method.

The GetMimeType utility is defined as follows:

local procedure GetMimeType(selectedFile: Text): Text
var
fileMgt: Codeunit "File Management";
mimeType: Text;
begin
case lowercase(fileMgt.GetExtension(selectedFile)) of
'pdf':
mimeType := 'application/pdf';
'txt':
mimeType := 'text/plain';
'csv':
mimeType := 'text/csv';
'png':
mimeType := 'image/png';
'jpg':
mimeType := 'image/jpg';
'bmp':
mimeType := 'image/bmp';
'gif':
mimeType := 'image/gif';
else
Error('File Format not supported!');
end;
EXIT(mimeType);
end;

This simply receives a filename as input, retrieves the file extension, and then returns the MIME type associated with the file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.147.53