
Hello everyone,
I'm developing as extension in Business Central (SaaS - so no File Management codeunit or internal C/AL code available).
The customer needs a procedure which reads a file they have hosted in their machines and changes some values in the Item Card based on what the file contains.
Since the file comes from a 3rd party software, the format can't be changed.
As I can't use any internal functions (such as File Management Codeunit or XML Ports with upload / download features), how can I write a function which simply allows the user to select a file from their PC and gives me the content as InStream / Txt o others?
Many thanks for your help,
Michael
hi,
Open existing File Attachment:
procedure OpenAttachment(pFileAttachmentEntryNo: Integer)
var
AttachmentRec: record Attachment;
ResponseStream: InStream;
tempfilename: text;
ErrorAttachment: Label 'No file available.';
begin
if AttachmentRec.get(pFileAttachmentEntryNo) then
if AttachmentRec."Attachment File".HasValue then begin
AttachmentRec.CalcFields("Attachment File");
AttachmentRec."Attachment File".CreateInStream(ResponseStream);
tempfilename := CreateGuid() + '.' + AttachmentRec."File Extension";
DOWNLOADFROMSTREAM(ResponseStream, 'Export', '', 'All Files (*.*)|*.*', tempfilename);
end
else
Error(ErrorAttachment);
end;
Upload new attachment:
procedure UploadAttachment()
var
AttachmentRec: Record Attachment;
FileOutStream: OutStream;
FileInStream: InStream;
tempfilename: text;
DialogTitle: Label 'Please select a File...';
begin
if UploadIntoStream(DialogTitle, '', 'All Files (*.*)|*.*', tempfilename, FileInStream) then begin
AttachmentRec.Init();
AttachmentRec.Insert(true);
AttachmentRec."Storage Type" := AttachmentRec."Storage Type"::Embedded;
AttachmentRec."Storage Pointer" := '';
AttachmentRec."File Extension" := GetFileType(tempfilename);
AttachmentRec."Attachment File".CreateOutStream(FileOutStream);
CopyStream(FileOutStream, FileInStream);
AttachmentRec.Modify(true);
end;
end;