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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Download PDF URL and Upload that PDF to Attachment using Code

(0) ShareShare
ReportReport
Posted on by 290

Hi Experts,

I have one URL from which I can download my pdf file and can manually attach this PDF to my Sales order Attachment.

For attaching PDF to SO attachment I'm following this link as reference:

https://www.tech.alirazazaidi.com/document-attachment-through-x-code-dynamics-ax-2012-r3/

And for Downloading URL I'm using this link as reference:

http://dev.goshoom.net/2016/03/file-upload-and-download-in-ax-7/

Browser browser = new Browser();
browser.navigate(fileUrl);// This code will ask to download the pdf file to particular location

TO DO: I want to Download this file and upload to the Sales order attachment.

Can anyone suggest me how to achieve this.

Thanks

Tanmay

I have the same question (0)
  • Verified answer
    Sumit Loya Profile Picture
    2,230 on at

    Hi Tanmay,

    Browsing the URL will not work as it will prompt the user for manual download. What you will have to do is to convert the PDF from URL into stream and then use that stream to attach the file to SO.

    Following code could probably help you in converting your file URL into stream.

    private static Stream ConvertToStream(string fileUrl)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
        try {
            MemoryStream mem = new MemoryStream();
            Stream stream = response.GetResponseStream();
    
            stream.CopyTo(mem,4096);
    
            return mem;
        } finally {
            response.Close();
        }
    }

  • Tanmay Upgade Profile Picture
    290 on at

    Hi Sumit,

    I tried that URL and used same code as you suggested but in stream I'm not getting the pdf file contents.

    Can you please guide me on this.

    Thanks,

    Tanmay

  • D365 Developer Profile Picture
    215 on at

    Hi Tanmay,

    How do you know details about PDF content from memory stream? The above suggestion from Sumit should work.

  • Verified answer
    Sumit Loya Profile Picture
    2,230 on at

    Hi Tanmay,

    Try this working x code. I have tested this out.

    public static void main(Args _args)
        {
            str fileUrl = 'https://www.civilaviation.gov.in/sites/default/files/State_wise_quarantine_regulation-converted.pdf';
            System.Net.HttpWebRequest request = System.Net.WebRequest::Create(fileUrl) as System.Net.HttpWebRequest;
            System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
            Filename        fName = 'Sample.pdf';
            Filename        attachmentName = System.IO.Path::GetFileNameWithoutExtension(fName);
            str fileContentType = System.Web.MimeMapping::GetMimeMapping(fName);
            SalesTable      salesTable;
            DocuRef         docuRef;
            try
            {
                System.IO.Stream stream = response.GetResponseStream();
                System.IO.MemoryStream  memStream = new System.IO.MemoryStream();
                stream.CopyTo(memStream);
                select firstonly salesTable;
    
                docuRef = DocumentManagement::attachFile(
                    salesTable.TableId,
                    salesTable.RecId,
                    salesTable.DataAreaId,
                    DocuType::typeFile(),
                    memStream,
                    fName,
                    fileContentType,
                    attachmentName);
    
                if (docuRef.RecId)
                {
                    info("File attached");
                }
            }
            finally
            {
                response.Close();
            }
        }

    See the screen shot below:

    pastedimage1620730360492v1.png

  • Tanmay Upgade Profile Picture
    290 on at

    HI Sumit,

    Thanks for the code, I tried the previous code again and it worked for me.

    Thank you once again.

    Regards,

    Tanmay

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans