Hi, Readers.
Today I would like to talk about how to export all Media Contents from table 2000000184 “Tenant Media” in Business Central.
As you might know, table fields support two data types for adding media to records: Media and MediaSet. With these data types, you can import media directly from a file to a record, or media can be passed to the record in an InStream object. Imported media is stored as an object in the system table 2000000184 Tenant Media of the tenant database. Each media object is assigned a unique identifier (ID).
For example, Picture field in table 27 Item.
PS: We can also use a BLOB data type. But Using the Media or MediaSet data type provides better performance than using a BLOB data type and is more flexible in its design. With a BLOB data type, each time the media is rendered in the client, it’s retrieved from the SQL database server, which requires extra bandwidth and affects performance. With the Media and MediaSet data types, the client uses media ID to cache the media data, which in turn improves the response time for rendering the media in the user interface.
Let’s get back to the topic. We can find this table on the Table Information page.
And, after clicking No. of Records, we can see the all records inside.
The Content field is a BLOB type in table 2000000184 “Tenant Media”. The content of this field is our target this time.
Let’s try exporting one first. (To make it easier to test on the page, I added an action to the Customer List.)
It looks fine.
Source Code:
pageextension 50100 MyExtension extends "Customer List"
{
actions
{
addafter(Email)
{
action(ExportAllTenantMedia)
{
Caption = 'Export Tenant Media';
Promoted = true;
PromotedCategory = Process;
PromotedIsBig = true;
Image = Export;
ApplicationArea = All;
trigger OnAction()
var
TenantMedia: Record "Tenant Media";
InS: InStream;
OutS: OutStream;
FileMgt: Codeunit "File Management";
TempBlob: Codeunit "Temp Blob";
FileName: Text[50];
begin
TenantMedia.Reset();
if TenantMedia.FindFirst() then
if TenantMedia.Content.HasValue then begin
TenantMedia.CalcFields(Content);
TenantMedia.Content.CreateInStream(InS);
TempBlob.CreateOutStream(OutS);
CopyStream(OutS, InS);
FileName := 'Example.jpg';
FileMgt.BLOBExport(TempBlob, FileName, true);
end;
end;
}
}
}
}
Then we need to export all of them, the best way is to combine them into a zip file. We have previously discussed how to add multiple files into a zip file. So I won’t repeat it this time. More details: Link
Let’s start.
Great.
We successfully exported all the files.
Test Video:
Source Code:
pageextension 50100 MyExtension extends "Customer List"
{
actions
{
addafter(Email)
{
action(ExportAllTenantMedia)
{
Caption = 'Export Tenant Media';
Promoted = true;
PromotedCategory = Process;
PromotedIsBig = true;
Image = Export;
ApplicationArea = All;
trigger OnAction()
var
TenantMedia: Record "Tenant Media";
InS: InStream;
OutS: OutStream;
FileMgt: Codeunit "File Management";
TempBlob: Codeunit "Temp Blob";
FileName: Text[50];
ZipFileName: Text[50];
DataCompression: Codeunit "Data Compression";
begin
TenantMedia.Reset();
TenantMedia.SetRange("Company Name", CompanyName);
//TenantMedia.SetFilter("Mime Type", 'image/png|image/jpeg');
DataCompression.CreateZipArchive();
FileName := '';
ZipFileName := Format(CurrentDateTime) + '.zip';
if TenantMedia.FindSet() then
repeat
if TenantMedia.Content.HasValue then begin
TenantMedia.CalcFields(Content);
TenantMedia.Content.CreateInStream(InS);
FileName := TenantMedia.ID + '.jpg';
DataCompression.AddEntry(InS, FileName);
end;
until TenantMedia.Next() = 0;
TempBlob.CreateOutStream(OutS);
DataCompression.SaveZipArchive(OutS);
TempBlob.CreateInStream(InS);
DownloadFromStream(InS, '', '', '', ZipFileName);
end;
}
}
}
}
PS:
1. You can add some filters before generating the file.
2. For the Media (MIME) types: The media type, also referred to as the MIME (Multipurpose Internet Mail Extensions) type, is an Internet standard to describe the contents of a file. Internet browsers use the MIME type to determine how to handle the file. The Media and MediaSet datatypes support all recognized MIME types.
A MIME type is defined by two parts, the type and subtype, where the format is type/subtype
. For example, the MIME type for a JPEG image is image/jpeg. There are several types, including image, application, audio, video, text, and more. Each MIME type is associated with one or more acceptable file extensions. The following table lists some of the more common MIME types and their file extensions.
MIME type | File extension |
---|---|
image/bmp | bmp |
image/jpeg | jpeg, jpg, jpe |
image/gif | gif |
application/msword | doc |
application/octet-stream | json |
application/vnd.openxmlformats-officedocument.wordprocessingml.document | docx |
application/vnd.ms-excel | xls |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | xlsx |
application/vnd.ms-powerpoint | ppt |
application/vnd.openxmlformats-officedocument.presentationml.presentation | pptx |
application/pdf | |
application/xml | xml |
audio/mpeg | mp3 |
audio/x-wav | wav |
video/mp4 | mp4 |
video/x-msvideo | avi |
text/html | htm, html |
text/plain | txt |
So the most correct way is to generate different File Extensions according to the MIME type of the current record. I just generated jpg files this time to save time. (Better not follow my example😑)
Find more about Working With Media on Records in MS Docs.
END
Hope this will help.
Thanks for reading.
ZHU
コメント