Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Copy newly created temp file

(1) ShareShare
ReportReport
Posted on by 551

Hi experts,

in NAV 2016 I have made a web service codeunit that creates a pdf file in the temp folder.

After DOWNLOADFROMSTREAM, I try to copy the file to another folder, but I get the error (manually translated): "The user does not have the required permission or the path is write protected".

I guess, I need to close a file or a stream, but cannot find the right one.

It works fine, if I copy any other files to the folder, that gives the error. So it must be something with the temp file.

Here is my code. Hope you can help!

var:

TempBlob: record TempBlob.

InS: Instream

OutS: OutStream

TempFileName: Text

EndingFileName: Text

TempBlob.RESET();
TempBlob.Blob.CREATEOUTSTREAM(OutS);

REPORT.SAVEAS(206, TempText, REPORTFORMAT::Pdf, OutS);
TempBlob.INSERT();
TempBlob.CALCFIELDS(Blob);
TempBlob.Blob.CREATEINSTREAM(InS);

TempFileName := 'Invoice.pdf';

DOWNLOADFROMSTREAM(InS,'','<TEMP>', '', TempFileName);

TempBlob.RESET();  // that did not help
CLEAR(InS);  // that did not help
CLEAR(OutS);  // that did not help

EndingFileName := '\\...\myShare$\' + 'Invoice.pdf';

COPY(TempFileName, EndingFileName);

  • Suggested answer
    Morten Steengaard Profile Picture
    551 on at
    RE: Copy newly created temp file

    Hi Nitin.

    Thank you very much for your answer.

    On another forum, someone told me to write this, and that works:

    DOWNLOADFROMSTREAM(InS,'','<TEMP>', '', TempFileName);

    EndingFileName := '\\...\myShare$\' + 'Invoice.pdf';

    FileManagement.CopyClientFile(TempFileName, EndingFileName, TRUE);

    FileManagement.DeleteClientFile(TempFileName);

    The problem was that COPY is working on the server and the TempFileName is on the client.

    But, thank you for your time!

    Best regards,

    Morten

  • Suggested answer
    Nitin Verma Profile Picture
    21,384 Moderator on at
    RE: Copy newly created temp file

    First you need to save your file into ServerTemp folder like below

    FileMgt : Codeunit "File Management";

    Pfile : File;

    ltServerFileName := FileMgt.ServerTempFileName('pdf');

    pFile.TEXTMODE := FALSE;

    pFile.WRITEMODE := TRUE;

    pFile.CREATE(ltServerFileName);

    pFile.CREATEOUTSTREAM(loOutStream);

    File.COPY(TempFileName, EndingFileName)

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,758 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans