web
You’re offline. This is a read only version of the page.
close
Skip to main content
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 555

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);

I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,684 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)

  • Suggested answer
    Morten Steengaard Profile Picture
    555 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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 4,327

#2
Sumit Singh Profile Picture

Sumit Singh 2,734

#3
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 2,599

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans