RE: Attaching Receipts in EP Expense Report error " File upload failed. File write error on server "
I am attaching xpo from my application and which is having some changes when compared to your code and check which may helps you.
#define.BUFFER_SIZE(4096)
server static DocuValue saveWebDocument(DocuRef docuRef, IISPostedFile postedFile)
{
BinData binData = new BinData();
int fileLength;
str filePath;
DocuType docuType;
DocuValue docuValue;
int dataLen;
container data;
FileIOPermission filePermission;
boolean fileWriteError;
str fileName, fileNameWithoutType;
System.Uri fileUri;
SharePointAuthenticationType authType;
Microsoft.Dynamics.AX.Framework.OfficeAddin.SharePoint.AuthenticationType docuAuthType;
Binary tempFile;
System.IO.MemoryStream tempStream = null;
System.Exception ex;
str endSlash(str _str)
{
return (strEndsWith(_str, '/') || strEndsWith(_str, '\\')) ? _str : (_str '/');
}
docuType = docuRef.docuType();
if (postedFile)
{
fileLength = postedFile.contentLength();
if (docuType.TypeGroup != DocuTypeGroup::Note)
{
if (fileLength > 0)
{
if (Docu::validateFileSize(docuType, fileLength))
{
docuValue.initFromDocuRef(docuRef);
// Encode the File name before saving it.
fileName = new IISServer().htmlEncode(postedFile.fileName());
docuValue.OriginalFileName = Docu::getFileName(fileName);
[docuValue.FileName, docuValue.FileType] = Docu::splitFilename(docuValue.OriginalFileName);
fileNameWithoutType = docuValue.FileName;
if (Docu::validateExtension(docuValue.FileType))
{
if (docuType.FilePlace == DocuFilePlace::Database)
{
// Database save is not efficient, loads entire file into memory
// Database limits on filesize should be kept low
[dataLen, data] = postedFile.read(fileLength);
docuValue.File = data;
docuValue.insert();
}
else if (docuType.FilePlace == DocuFilePlace::SharePoint)
{
docuValue.Type = DocuValueType::URL;
// Overwrite the filename with a generated one
docuValue.FileName = smmDocuments::getNonExistingFileName(NumberSeq::newGetNum(DocuParameters::numRefDocuNumber(),true), docuRef.path() fileNameWithoutType #FileNameDelimiter, docuValue.FileType);
filePath = endSlash(docuRef.path()) fileNameWithoutType #FileNameDelimiter docuValue.FileName '.' docuValue.FileType;//keep the ep file name same with client file name
docuValue.Path = filePath;
// Check the authentication type
authType = DocuSharePointParameters::GetAuthenticationType(filePath);
if (authType != SharePointAuthenticationType::SharePointOnline)
{
try
{
// SharePoint save is not efficient, loads entire file into memory
// Limits on filesize should be kept low
[dataLen, data] = postedFile.read(fileLength);
tempFile = Binary::constructFromContainer(data);
tempStream = tempFile.getMemoryStream();
docuAuthType = DocuSharePointParameters::ConvertAuthenticationType(authType);
fileUri = new System.Uri(filePath);
Microsoft.Dynamics.AX.Framework.OfficeAddin.SharePoint.DocumentUploader::UploadDocument(tempStream, fileUri, docuAuthType, false);
docuValue.insert();
// Clean up the temporary memory stream used
tempStream.Close();
tempStream.Dispose();
tempStream = null;
}
catch (Exception::CLRError)
{
ex = CLRInterop::getLastException();
if (ex)
{
if (ex.get_InnerException())
{
error(ex.get_Message());
ex = ex.get_InnerException();
}
throw error(ex.get_Message());
}
}
}
else
{
// Authenticating to SharePoint Online currently requires UI to be displayed
error("@SYS4010239");
}
}
else
{
// Overwrite the filename with a generated one
docuValue.FileName = smmDocuments::getNonExistingFileName(NumberSeq::newGetNum(DocuParameters::numRefDocuNumber(),true), docuRef.path(), docuValue.FileType);
filePath = docuRef.path() docuValue.FileName '.' docuValue.FileType;
// File overwrite is not allowed
filePermission = new FileIOPermission(filePath, 'rw');
filePermission.assert();
// WinApiServer::fileExists will demand read access
// BP Deviation Documented
if (!WinAPIServer::fileExists(filePath))
{
fileWriteError = false;
while(true)
{
[dataLen, data] = postedFile.read(#BUFFER_SIZE);
if (dataLen == 0)
{
break;
}
binData.setData(data);
// Assert for write CAS permission has been done
// BP Deviation Documented
if (!binData.appendToFile(filePath))
{
fileWriteError = true;
break;
}
}
if (!fileWriteError)
{
docuValue.insert();
}
else
{
// Try to delete any partially written files
// Assert for write CAS permission has been done
// BP Deviation Documented
WinAPIServer::deleteFile(filePath);
// File write error on server
error("@SYS113524");
}
}
else
{
warning("@SYS97284");
}
}
}
else
{
warning("@SYS97285");
}
}
else
{
warning("@SYS97286");
}
}
else
{
warning("@SYS97287");
}
}
else
{
warning("@SYS77591");
}
}
else
{
throw error(Error::missingParameter(null));
}
return docuValue;
}
Also look in to this post which is similar to your issue :- https://community.dynamics.com/ax/f/microsoft-dynamics-ax-forum/161202/error-when-attempting-ep-receipt-upload