using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;
using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;
using Microsoft.Dynamics.AX.Framework.FileManagement;
public class AttachFile
{
str docfiletype;
Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider;
public boolean createAttachment(TableId tableId, RefRecId _refFieldId,Filename _Filename,Filename _filePath)
{
boolean ret = false;
DocuRef docuref;
str downloadUrl;
System.IO.Stream _stream;
_stream = File::UseFileFromURL(_filePath);
str _contentType = System.Web.MimeMapping::GetMimeMapping(_filePath);
DocuType fileType = DocuType::find(DocuType::typeFile());
storageProvider = Docu::GetStorageProvider(fileType, true, curUserId());
if(storageProvider)
{
str uniqueFileName = storageProvider.GenerateUniqueName(_Filename);
str fileNameWithoutExtension = System.IO.Path::GetFileNameWithoutExtension(_filePath);
str fileExtension = Docu::GetFileExtension(uniqueFileName);
if(Docu::validateExtension(fileExtension))
{
guid FileId = newGuid();
DocuValue docValue;
docValue.Name = fileNameWithoutExtension;
docValue.FileId = FileId;
docValue.FileName = uniqueFileName;
docValue.FileType = fileExtension;
docValue.OriginalFileName = _Filename;
docValue.Type = DocuValueType::Others;
docValue.StorageProviderId = storageProvider.ProviderId;
DocumentLocation 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();
//info(docValue.AccessInformation);
}
if (docValue.validateWrite())
{
docValue.insert();
DocuUploadResult DocuUploadResult = new DocuUploadResult(_fileName, _contentType, false, "", newGuid());
DocuUploadResult.fileId(FileId);
docuref = DocuUploadResult.createDocuRef(tableId,_refFieldId,DocuType::typeFile());
if(docuref)
{
ret = true;
}
else
{
ret = false;
}
}
}
}
}
No comments:
Post a Comment