public DocuRef attachFile(purchtable_purchTable, System.IO.Stream _stream, str _filePath, str _attachmentName, DocuType _docuType, FileId _fileId)
{
DocuRef ret;
if (!_purchTable.RecId || !_docuType.RecId || !_attachmentName || !_stream || !_filePath)
{
throw error(error::missingParameter(null));
}
if (_stream.CanSeek)
{
_stream.Seek(0, System.IO.SeekOrigin::Begin);
}
if (!Docu::validateFileSize(_docuType, _stream.Length))
{
throw error("file size is large");
}
try
{
using (System.IO.MemoryStream localStream = new System.IO.MemoryStream())
{
_stream.CopyTo(localStream);
if (localStream.CanSeek)
{
localStream.Seek(0, System.IO.SeekOrigin::Begin);
}
ttsbegin;
ret.RefTableId = DocuRef::GetRootTableID(_purchTable.TableId);
ret.RefRecId = _purchTable.RecId;
ret.RefCompanyId = _purchTable.DataAreaId;
ret.TypeId = _docuType.TypeId;
ret.Name = _attachmentName;
ret.insert();
boolean result = this.Attachment( ret, _docuType,_filePath,System.Web.MimeMapping::GetMimeMapping(_filePath),
localStream,_fileId);
if (!result)
{
throw error("ErrorWhileAttaching");
}
ttscommit;
}
}
catch
{
// Optional: add error handling or logging if needed
throw;
}
return ret;
}
public boolean Attachment(DocuRef _docuRef, DocuType _docuType, str _fileName, str _contentType, System.IO.Stream _fileContents, FileId _fileId)
{
boolean ret = false;
str fileExtension = Docu::GetFileExtension(_fileName);
if (fileExtension && !Docu::validateExtension(fileExtension))
{
return checkFailed(strFmt("@ElectronicReporting:AttachmentTypeIsNotAllowed", strFmt('.%1', fileExtension)));
}
DocuUploadResult result = this.createDocuValueTabe(_fileContents, _fileName, _docuType, _contentType, _docuRef.Name, _fileId);
if (result.getUploadStatus())
{
DocuValue docuValue = DocuValue::findByFileId(result.fileId());
_docuRef.selectForUpdate(true);
_docuRef.ValueRecId = docuValue.RecId;
_docuRef.update();
ret = true;
}
else
{
throw error("@ElectronicReporting:FileTemplateNotFound");
}
return ret;
public FileUploadResultBase createDocuValueTabe( System.IO.Stream _stream, str _fileName,DocuType _docuType,
str _contentType = null,Description _name = '', FileId _fileId = newGuid())
{
Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider =
Docu::GetStorageProvider(_docuType, true, curUserId());
if (storageProvider)
{
str uniqueFileName = storageProvider.GenerateUniqueName(_fileName);
str fileNameWithoutExtension = System.IO.Path::GetFileNameWithoutExtension(uniqueFileName);
str fileExtension = Docu::GetFileExtension(uniqueFileName);
str name = _name ? _name : fileNameWithoutExtension;
if (Docu::validateExtension(fileExtension))
{
DocuValue docValue;
docValue.Name = name;
docValue.FileId = _fileId;
docValue.FileName = fileNameWithoutExtension;
docValue.FileType = fileExtension;
docValue.OriginalFileName = _fileName;
docValue.Type = DocuValueType::Others;
docValue.StorageProviderId = storageProvider.ProviderId;
if (_stream.CanSeek)
{
_stream.Seek(0, System.IO.SeekOrigin::Begin);
}
DocumentLocation location;
var overwritableStorageProvider = storageProvider as IOverwriteableDocumentStorageProvider;
if (overwritableStorageProvider != null)
{
location = overwritableStorageProvider.SaveFileWithOverwrite(docValue.FileId, uniqueFileName, _contentType, _stream);
}
else
{
location = storageProvider.SaveFile(docValue.FileId, uniqueFileName, _contentType, _stream);
}
if (location != null)
{
if (location.NavigationUri)
{
docValue.Path = location.get_NavigationUri().ToString();
}
if (location.AccessUri)
{
docValue.AccessInformation = location.get_AccessUri().ToString();
}
if (docValue.validateWrite())
{
docValue.insert();
return new DocuUploadResult(_fileName, _contentType, true, '', _fileId);
}
}
}
}
return null;
}