Loading...

X++ Upload and Download file - D365 FO

X++ Upload and Download file - D365 FO
class RetailSharedParameters_Eventhandlers
{
    /// <summary>
    /// Event handler for the "UploadFile" button click event.
    /// </summary>
    /// <param name="sender">The form control that triggered the event.</param>
    /// <param name="e">Event arguments.</param>
    [FormControlEventHandler(formControlStr(RetailSharedParameters, UploadFile), FormControlEventType::Clicked)]
    public static void UploadFile_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        // Get the button that triggered the event
        FormButtonControl callerButton = sender as FormButtonControl;
        
        // Get the form run instance
        FormRun form = callerButton.formRun();
        
        // Get the data source for the RetailSharedParameters table
        FormDataSource RetailSharedParameters_ds = form.dataSource(formDataSourceStr(RetailSharedParameters, RetailSharedParameters)) as FormDataSource;
        
        // Get the current record from the data source
        RetailSharedParameters RetailSharedParameters = RetailSharedParameters_ds.cursor();

        // Prompt the user to upload a file
        FileUploadTemporaryStorageResult result = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
        
        // Check if the file upload was successful
        if (result && result.getUploadStatus())
        {
            // Store the file name and download URL in the table
            RetailSharedParameters.FileName = result.getFileName();
            RetailSharedParameters.SoundFile = result.getDownloadUrl();
            
            // Save the changes to the database
            RetailSharedParameters.write();
        }
    }

    /// <summary>
    /// Event handler for the "DownloadFile" button click event.
    /// </summary>
    /// <param name="sender">The form control that triggered the event.</param>
    /// <param name="e">Event arguments.</param>
    [FormControlEventHandler(formControlStr(RetailSharedParameters, DownloadFile), FormControlEventType::Clicked)]
    public static void DownloadFile_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        // Variable to store the file URL
        str fileUrl = '';
        
        // Get the button that triggered the event
        FormButtonControl callerButton = sender as FormButtonControl;
        
        // Get the form run instance
        FormRun form = callerButton.formRun();
        
        // Get the data source for the RetailSharedParameters table
        FormDataSource RetailSharedParameters_ds = form.dataSource(formDataSourceStr(RetailSharedParameters, RetailSharedParameters)) as FormDataSource;
        
        // Get the current record from the data source
        RetailSharedParameters RetailSharedParameters = RetailSharedParameters_ds.cursor();
        
        // Retrieve the stored file URL
        fileUrl = RetailSharedParameters.SoundFile;
        
        // Open the file URL in a browser
        if (fileUrl)
        {
            new Browser().navigate(fileUrl);
        }
        else
        {
            // Notify the user if no file URL is found
            warning("No file URL found.");
        }
    }
}

Published on:

Learn more
Need help with this product?

We can help you with X++ Upload and Download file - D365 FO

If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.

Sherif Fayed
Sherif Fayed

Share post:

Related posts

D365 Sending Email with Customer Account Statement SSRS report as attachment using X++

D365 Sending Email with Customer Account Statement SSRS report as attachment using X++ custTable _custTable;        &...

2 years ago

clicking link on info message X++ to Open form

 Message::AddAction() method can be used to embed an action within a message sent to the message bar. This method supports adding a singl...

2 years ago

D365 FO - Send Email with attachments by X++

 Go to System administration >> Setup >> Email >> System email templatesCreate New template [ExtensionOf(clas...

2 years ago

D365 FO Data entity Export using X++

This post provides information on exporting data entity through X++ in D365 FO. The post includes a code snippet written in X++ that can be us...

2 years ago

D365 FO Encryption And Decryption Using A Symmetric Key(AES) using X++

If you're looking to encrypt and decrypt data using a symmetric key in D365 FO, this tutorial is the right place to be. The post details the s...

2 years ago

Get multiple selected rows using D365 X++

This blog post offers a valuable insight into D365 X++, offering a detailed guide on how to get multiple selected rows. Multi-select for Butto...

2 years ago

D365 Solution for Cloud Dev machine show error ' The remote certificate is invalid according to the validation procedure. '

 You are getting 'The remote certificate is invalid according to the validation procedure.'Error show with all reports and  tired Ro...

2 years ago

D365 F&O setup email notifications on workflows

 How to setup email notifications on workflows in D365 F&OToday we’re going to see how to manage workflow notifications in D365 Suppl...

3 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.