Modifier

Partager via


File.DownloadFromStream(InStream, Text, Text, Text, var Text) Method

Version: Available or changed with runtime version 1.0.

Sends a file from server computer to the client computer. The client computer is the computer that is running the Windows client or the computer that is running the browser that accesses the web client.

Syntax

[Ok := ]  File.DownloadFromStream(InStream: InStream, DialogTitle: Text, ToFolder: Text, ToFilter: Text, var ToFile: Text)

Note

This method can be invoked without specifying the data type name.

Parameters

InStream
 Type: InStream
An InStream that you want to use to send the data in a file on Business Central Server to a file on the client computer.

DialogTitle
 Type: Text
The title that you want to display in the dialog box for downloading the file. This parameter is not supported by the web client. The title is determined by the end-user's browser.

ToFolder
 Type: Text
The default folder in which to save the file to be downloaded. The folder name is displayed in the dialog box for downloading the file. The folder can be changed by the user. This parameter is not supported by the web client. By default, files are saved to the default download location that is configured in the end-user's browser.

ToFilter
 Type: Text
The type of file that can be downloaded to the client computer. The type is displayed in the dialog box for downloading the file. This parameter is not supported by the web client.

ToFile
 Type: Text
The name to give the downloaded file. This is the default file name that is shown in the dialog box for downloading the file. This value, can be changed by the user.

Return Value

[Optional] Ok
 Type: Boolean
true if the operation was successful; otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.

Remarks

Note

On devices that run Apple iOS, such as iPad, you can only download a file if the Apple iOS device on which you are downloading the file has an application that supports the file type.

The business logic runs on the computer that is running Dynamics 365 Business Central service and not on the client. Files are created on a Dynamics 365 service and not locally on the client computer. When you write code, you must consider where files are created.

Use Upload Method (File) and UploadIntoStream Method (File) to send a file from a client to a Dynamics 365 Business Central service instance.

Use Download Method (File) and DownloadFromStream Method (File) to send a file from a Dynamics 365 Business Central service instance to a client.

We recommend that you use the methods in codeunit 419 File Management to upload and download files on-premises.

Note

Internet browsers can only handle one file per request. Therefore, with the Web client, if this method is called in a repetitive statement (or loop) that generates multiple files, only the last file will be sent to the browser. Alternatively, when designing for the Web client, bundle the files in an archive file (.zip), for example, by using the methods found in codeunit 419 File Management. For more details about this design pattern, see Multi-File Download. The methods in codeunit 419 aren't external, therefore can't be used in extensions. Instead, when developing extensions in AL, use the external methods of codeunit 425 Data Compression. The approach is similar.

Example - Cloud

procedure DownloadFromCloud()
    var
        Data: BigText;
        ins: InStream;
        outs: OutStream;
        TempBLOB: codeunit "Temp Blob";
        filename: Text;
    begin
        Data.AddText('Hello World');
        TempBLOB.CreateOutStream(outs);
        Data.Write(outs);
        TempBLOB.CreateInStream(ins);
        filename := 'helloworld.txt';
        DownloadFromStream(
            ins,  // InStream to save
            '',   // Not used in cloud
            '',   // Not used in cloud
            '',   // Not used in cloud
            filename); // Filename is browser download folder
    end;

Example - On-premises

 var
    TempFile: File;
    NewStream: InsTream;
    ToFileName: Variant;
begin  
    TempFile.CreateTempFile();  
    TempFile.Write('abc');  
    TempFile.CreateInStream(NewStream);  
    ToFileName := 'SampleFile.txt';  
    DownloadFromStream(NewStream,'Export','','All Files (*.*)|*.*',ToFileName);
end;

File Data Type
Get Started with AL
Developing Extensions