수색…


소개

이 항목에서는 계약 기반 API를 사용하여 Acumatica ERP 내부의 세부 엔터티에 첨부 된 파일을 다운로드하는 방법을 보여줍니다.

비고

위의 코드는 Json.NET 프레임 워크 ( Newtonsoft.Json.dll )를 사용하여 작성되었습니다.

SOAP 응답에서 HTTP 쿠키 헤더를 얻으려면 .NET Framework System.ServiceModelSystem.ServiceModel.Web 어셈블리에 대한 참조를 추가하고 코드 파일에 다음 2 가지 지시문을 사용합니다.

using System.ServiceModel;
using System.ServiceModel.Web;

SOAP 및 REST 클라이언트가 공유하는 SOAP 응답의 HTTP 쿠키 헤더

Acumatica의 SOAP Contract-Based API에는 최상위 엔티티에 대해서만 첨부 파일을 다운로드 할 수있는 제한이 있습니다. GetFiles () 메서드를 사용하여 세부 엔터티의 첨부 파일을 가져 오려는 시도는 "불행히도 화면 바인딩없는 엔터티를 최상위 엔터티로 사용할 수 없습니다. "라는 오류가 발생합니다.이 대화 상자는 최상위 엔터티 로만 사용할 수 있습니다 . 웹 서비스 엔드 포인트에 정의 된 상위 레벨 엔티티.

GetFiles () 메서드의 또 다른 한계는 엔터티에 연결된 모든 파일의 내용을 항상 반환한다는 것입니다. 먼저 파일 이름 만 검색 한 다음 Acumatica에서 다운로드 할 특정 파일을 결정할 수있는 옵션이 없습니다.

고맙게도 Contract-Based REST API와 함께 제공되는 첨부 파일을 사용하여보다 효율적으로 제어 할 수있는 방법이 있습니다. 계약 기반 REST API에서 내 보낸 모든 엔티티의 일부로 반환 된 files 배열에는 다음 항목 만 포함됩니다.

  • 파일 이름 (파일 이름 속성)
  • 파일 식별자 ( id 속성)
  • 나중에 파일 내용을 다운로드 할 수있는 하이퍼 텍스트 참조 ( href 속성)

웹 서비스 엔드 포인트에서 엔티티에 첨부 된 파일 목록을 가져오고 계약 기반 REST API를 통해 특정 파일 컨텐츠를 검색하는 예제는 Acumatica Product Help 를 확인하십시오.

전체 통합 프로젝트가 SOAP 계약 기반 API로 개발 된 경우 어떻게 세부 엔터티에 첨부 된 파일을 다운로드 할 수 있습니까? 아래의 코드 스 니펫에서 볼 수 있듯이 SOAP 응답의 HTTP 쿠키 헤더를 첨부 파일 작업을 위해 독점적으로 사용되는 REST API 클라이언트에 전달할 수 있습니다.

using (var soapClient = new DefaultSoapClient())
{
    var address = new Uri("http://localhost/AcumaticaERP/entity/Default/6.00.001/");
    CookieContainer cookieContainer;
    using (new OperationContextScope(soapClient.InnerChannel))
    {
        soapClient.Login(login, password, null, null, null);
        string sharedCookie = WebOperationContext.Current.IncomingResponse.Headers["Set-Cookie"];
        cookieContainer = new CookieContainer();
        cookieContainer.SetCookies(address, sharedCookie);
    }
    try
    {
        var shipment = new Shipment()
        {
            ShipmentNbr = new StringSearch { Value = "001301" },
            ReturnBehavior = ReturnBehavior.OnlySpecified
        };
        shipment = soapClient.Get(shipment) as Shipment;

        var restClient = new HttpClient(
            new HttpClientHandler
            {
                UseCookies = true,
                CookieContainer = cookieContainer
            });
        restClient.BaseAddress = address;// new Uri("http://localhost/059678/entity/Default/6.00.001/");

        var res = restClient.GetAsync("Shipment/" + shipment.ID + "?$expand=Packages")
            .Result.EnsureSuccessStatusCode();
        var shipmentWithPackages = res.Content.ReadAsStringAsync().Result;

        JObject jShipment = JObject.Parse(shipmentWithPackages);
        JArray jPackages = jShipment.Value<JArray>("Packages");
        foreach (var jPackage in jPackages)
        {
            JArray jFiles = jPackage.Value<JArray>("files");
            string outputDirectory = ".\\Output\\";
            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }

            foreach (var jFile in jFiles)
            {
                string fullFileName = jFile.Value<string>("filename");
                string fileName = Path.GetFileName(fullFileName);
                string href = jFile.Value<string>("href");

                res = restClient.GetAsync(href).Result.EnsureSuccessStatusCode();
                byte[] file = res.Content.ReadAsByteArrayAsync().Result;
                System.IO.File.WriteAllBytes(outputDirectory + fileName, file);
            }
        }
    }
    finally
    {
        soapClient.Logout();
    }
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow