Epicor Rest API - Fails to Upload Image Files

Hi Team, I am getting the following error when images uploaded to epicor…Unable to POST to Ice.BO.AttachmentSvc/UploadFile - 0 Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. Note: It works fine for very small text file…

    //Read file from Disk into a byte array
    Byte[] bytes = Function.FileToByteArray(filePath);
    //while (stream.DataAvailable == true)
    //    bytes[i] = ((byte)stream.ReadByte());
    //    i++;
    //data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

    String fileData = Convert.ToBase64String(bytes);
    //int i = 0;
    //Create the File Object to be uploaded to Epicor 
    var file = new
        fileName = System.IO.Path.GetFileName(filePath),
        data = fileData,
        docTypeID = "ECO",
        parentTable = "ProjectPhase",
        metadata = new
            _Author = "Epicor PDF Importer",
            _TableName = "Erp.Project",
            _TableSysRowID = Guid.NewGuid()

    //System.Net.ServicePointManager.Expect100Continue = false;
    //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    //System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
    //Upload the file using the Attachment BO
    //EpicorRestV1.BearerToken = "";   //AsyncCallback

    var addedDocFile =  EpicorRestV1.DynamicPost("Ice.BO.AttachmentSvc", "UploadFile", file);
    //Create the Epicor XFileRef
    var xFileRef = new { Company = Company, XFileName = addedDocFile.ds.ToString(), XFileDesc = file.fileName, DocTypeID = "ECO" };
    var addedXRef = EpicorRestV1.DynamicPost("Ice.BO.XFileRefSvc", "XFileRefs", xFileRef);

    // Once the XFileRef exists attach it to a UD103 record.
    var ProjPhaseAttch = new
        Company = Company,
        ProjectID = ProjectID,
        PhaseID = PhaseID ,
        FileName = addedXRef.XFileName,
        DrawDesc = addedXRef.XFileDesc,
        DocTypeID = file.docTypeID,
        ForeignSysRowID = Guid.NewGuid(),               
        RowMod = "U"
    dynamic newUDAttch = EpicorRestV1.DynamicPost("Erp.BO.ProjectSvc", "ProjPhaseAttches", ProjPhaseAttch);

    //return strRetValue;

catch (Exception e)
    //Console.WriteLine(e + "");
    strResult = e.Message;

Hi… Any of you can advise on my query.

Generate your json and post it via swagger or postman.

Verify it’s not something with the library you are using.

thanks for reply… I am not sure what is causing issue but when i try as below, i could post document with out error…
{ var addedDocFile1 = EpicorRestV1.DynamicPost(“Ice.BO.AttachmentSvc”, “UploadFile”, null); }
{ }
var addedDocFile = EpicorRestV1.DynamicPost(“Ice.BO.AttachmentSvc”, “UploadFile”, file);

Always when i run first time, i get error when dynamicpost method called, so i tried to use try catch mechanism to come out from the error. following to that , i call same method and also pass file (bytes) parameter. It’s executing successfully.

I have one more query related to file attachment. Is there any method exposed to download the file from attachment.

Go to the REST help for that Attachment service and search for the methods containing the word “download”, there will be several:

1 Like

Thank you very much quick support… I used the following code to download file from file attachment table
var file = new
xFileRefNum = RefNumber
var docFile = EpicorRestV1.DynamicPost(“Ice.BO.AttachmentSvc”, “DownloadFile”, file);

if (((Newtonsoft.Json.Linq.JContainer)docFile).HasValues)
Byte[] byteArray = (Byte[])(((Newtonsoft.Json.Linq.JProperty)((Newtonsoft.Json.Linq.JContainer)docFile).First).Value);
File.WriteAllBytes(filePath, byteArray);