web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Saving two files using SendStringAsFileToUser only saves last file

(0) ShareShare
ReportReport
Posted on by 4,079

I need to save some data to two files in D365FO, I have made this code to show the problem.

public static void main(Args _args)
    { 
        str filePath,
         filePathAndName,
         fileContent = "01234567890123456789";
        ;
        // prepare file name
        filePath = System.IO.Path::GetTempPath();
        filePathAndName = filePath   'f'   guid2str(newGuid())   '.txt';
        // Save file
        File::SendStringAsFileToUser(fileContent, filePathAndName);

        fileContent = "MY New Content";
        File::SendStringAsFileToUser(fileContent, filePathAndName);

        info(filePath);
        info(filePathAndName);
        info("done");
    }

In this case only the last file is saved.

Any tips to how I can save data to two different files in D365FO?

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,660 Most Valuable Professional on at

    A solution may be opening the files in new tabs. Here is a quick & dirty implementation:

    // Creating files
    File::SendStringAsFileToUserNewTab("01234567890123456789", 'file1.txt');
    File::SendStringAsFileToUserNewTab("MY New Content", 'file2.txt');
    info("done");
    
    // New methods of File class
    [ExtensionOf(classStr(File))]
    final class My_File_Extension
    {
        public static void SendStringAsFileToUserNewTab(str content, str fileName, System.Text.Encoding encoding = System.Text.Encoding::get_UTF8(), ClassName fileUploadStrategyClassName = classstr(FileUploadTemporaryStorageStrategy))
        {
            System.Byte[] byteArray = encoding.GetBytes(content);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
    
            File::SendFileToUserNewTab(stream, fileName, fileUploadStrategyClassName);
        }
    
        public static void SendFileToUserNewTab(System.IO.Stream stream, str fileName, ClassName fileUploadStrategyClassName = classstr(FileUploadTemporaryStorageStrategy))
        {
            Browser br = new Browser();
            str downloadUrl;
            
            downloadUrl = File::SendFileToTempStore(stream, fileName, fileUploadStrategyClassName, true);
            if (downloadUrl != "")
            {
                br.navigate(downloadUrl, true, false);
            }
            else
            {
                warning("@ApplicationPlatform:DownloadFailed");
            }
        }
    
    }
     

  • Rudi Hansen Profile Picture
    4,079 on at

    Hi Martin.

    That does solve the problem.

    But it also leaves me with another question, should I even use the SendStringAsFileToUser function to start with, or is there some other way to get files to a user in D365FO? (Like send the files to ftp or email)

  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    The system can't see the users's workstation, and doesn't have access to it. So the only way to provide them directly to the user is to offer the files via the browser and let the user download them.

    But of course you can also use FTP or email.

  • Martin Dráb Profile Picture
    239,660 Most Valuable Professional on at

    Regarding email, you can use SysMailerMessageBuilder class to create an email message (addAttachment() is used for attachments) and send it with SysMailerFactory::sendInteractive() or SysMailerFactory::sendNonInteractive().

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 676

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 633 Super User 2026 Season 1

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 624 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans