final class DTPropertyDrawing { /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> [FormControlEventHandler(formControlStr(DTPropertyManagementSystemForm, PropertyDrawingButton), FormControlEventType::Clicked)] public static void PropertyDrawingButton_OnClicked(FormControl sender, FormControlEventArgs e) { DTProrpertyHeader dtProrpertyHeader; FormRun formRun = sender.formRun() as FormRun; FormDataSource dtProrpertyHeader_ds; DTPropertyDrawing attach = new DTPropertyDrawing(); TableId tableid; dtProrpertyHeader_ds = sender.formRun().dataSource("DTProrpertyHeader"); dtProrpertyHeader = dtProrpertyHeader_ds.cursor(); tableid = tableNum(DTProrpertyHeader); FileUploadTemporaryStorageResult result; result = file::GetFileFromUser(classStr(FileUploadTemporaryStorageStrategy)); System.IO.Stream sendfileStream = attach .createAttachment(tableid,dtProrpertyHeader.RecId,result); } ///Lets create the method “createAttachment()” which will do the main job of attaching the file corresponding to dtProrpertyHeader RecId. public System.IO.Stream createAttachment(TableId _refTableId, RefRecId _refRecId,FileUploadTemporaryStorageResult result) { DocuRef docuRefTable; DocuValue docuValueTable; Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation location; Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider; str firstFilePath, firstFileTempPath, firstFileBase64, firstFileName, firstFileExtension; System.IO.Stream FileStream ; String20 returnMsg; FileIOPermission permission = new FileIOPermission('', 'r'); permission.assert(); InteropPermission interopPerm = new InteropPermission(InteropKind::ClrInterop); interopPerm.assert(); DocuType fileType = DocuType::find(DocuType::typeFile()); storageProvider = Docu::GetStorageProvider(fileType, true, curUserId()); if (_refRecId > 0) { ttsBegin; docuValueTable.clear(); guid FileId = newGuid(); docuValueTable.FileId = FileId; docuValueTable.FileName = result.getFileName(); docuValueTable.Name = result.getFileName(); docuValueTable.OriginalFileName = result.getFileName();; container readcon = Binary::constructFromMemoryStream(result.openResult()).getContainer(); docuValueTable.File = readcon; FileStream = Binary::constructFromContainer(docuValueTable.File).getMemoryStream(); location = storageProvider.SaveFile( docuValueTable.FileId, storageProvider.GenerateUniqueName(docuValueTable.OriginalFileName), System.Web.MimeMapping::GetMimeMapping(docuValueTable.OriginalFileName), FileStream); if (location.NavigationUri) { docuValueTable.Path = location.get_NavigationUri().ToString(); } if (location.AccessUri) { docuValueTable.AccessInformation = location.get_AccessUri().ToString(); } docuValueTable.StorageProviderId = storageProvider.ProviderId; docuValueTable.insert(); if (docuValueTable.RecId) { docuRefTable.clear(); docuRefTable.RefRecId = _refRecid; docuRefTable.RefTableId = _refTableId; docuRefTable.RefCompanyId = curext(); docuRefTable.Name = result.getFileName(); docuRefTable.TypeId = 'File'; docuRefTable.ValueRecId = docuValueTable.RecId; docuRefTable.insert(); info("File Uploaded successfully"); } ttsCommit; } else { returnMsg = "Record not exit for the given Id"; } return FileStream; } /// The above code will save the file to blob. Now lets get the file to the stream using code. For this we need to write a method: public System.IO.Stream getFilestream(DocuValueFile _File) { System.IO.Stream FileStream2 = Binary::constructFromContainer(_File).getMemoryStream(); return FileStream2; } }
final class DTPropertyfileUpload { public static void main(Args _args) { FileUploadTemporaryStorageResult result; DTProrpertyHeader dtProrpertyHeader; result = File::GetFileFromUser(classStr(FileUploadTemporaryStorageStrategy)); if(result.getUploadStatus()) { str fileName = "YourFileName.jpeg"; //Insert the name of the attachchment using(System.IO.MemoryStream stream = result.openResult() as System.IO.MemoryStream) { DocuRef docuRef = DocumentManagement::attachFile( DTProrpertyHeader.TableId, DTProrpertyHeader.RecId, DTProrpertyHeader.DataAreaId, DocuType::typeImage(), stream, fileName, null, fileName); } } } }
if (!_refTableId || !_refRecId || !_refDataAreaId || !_type || !_attachmentName || _file == null || !_fileName) { throw error(error::missingParameter(null)); }
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 683 Most Valuable Professional
André Arnaud de Cal... 563 Super User 2025 Season 2
Sohaib Cheema 398 User Group Leader