Notifications
Announcements
No record found.
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
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(); } }
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,
How do you know details about PDF content from memory stream? The above suggestion from Sumit should work.
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:
HI Sumit,
Thanks for the code, I tried the previous code again and it worked for me.
Thank you once again.
Regards,
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Martin Dráb 660 Most Valuable Professional
André Arnaud de Cal... 549 Super User 2025 Season 2
Sohaib Cheema 307 User Group Leader