【问题标题】:acumatica import items with image APIacumatica 使用图像 API 导入项目
【发布时间】:2018-01-14 05:38:55
【问题描述】:

现在我已经成功地在 Acumatica 的 IN202500 屏幕中批量导入项目的代码(使用多个线程)。

问题是我正在努力导入一个项目的图像,实际上我自己没有图像,只有指向该图像的 URL 链接。

所以,我的问题是有人在 c# 中做过这个吗?

这是我的一段代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ItemImportMultiThreaded
{
    public class ItemImporter
    {
        private IN202500.Screen _itemsScreen;
        private static object _itemsSchemaLock = new object();
        private static IN202500.Content _itemsSchema;

        public void Login(string url, string username, string password, string company)
        {
            Console.WriteLine("[{0}] Logging in to {1}...", System.Threading.Thread.CurrentThread.ManagedThreadId, url);

            _itemsScreen = new IN202500.Screen();
            _itemsScreen.Url = url + "/PMSDB/(W(2))/Soap/IN202500.asmx";
            _itemsScreen.EnableDecompression = true;
            _itemsScreen.CookieContainer = new System.Net.CookieContainer();
            _itemsScreen.Timeout = 36000;
            _itemsScreen.Login(username, password);



            Console.WriteLine("[{0}] Logged in to {1}.", System.Threading.Thread.CurrentThread.ManagedThreadId, url);

            lock (_itemsSchemaLock)
            {
                // Threads can share the same schema.
                if (_itemsSchema == null)
                {
                    Console.WriteLine("[{0}] Retrieving IN202500 schema...", System.Threading.Thread.CurrentThread.ManagedThreadId);
                    _itemsSchema = _itemsScreen.GetSchema();
                    if (_itemsSchema == null) throw new Exception("IN202500 GetSchema returned null. See AC-73433.");
                }
            }
        }

        public void Logout()
        {
            _itemsScreen.Logout();
        }

        public void Import(List<Item> items)
        {
            Console.WriteLine("[{0}] Submitting {1} items to Acumatica...", System.Threading.Thread.CurrentThread.ManagedThreadId, items.Count);

            var commands = new IN202500.Command[]
            {
                _itemsSchema.StockItemSummary.InventoryID,
                _itemsSchema.StockItemSummary.Description,
                _itemsSchema.GeneralSettingsItemDefaults.ItemClass,
                _itemsSchema.VendorDetails.VendorID,
                _itemsSchema.VendorDetails.VendorInventoryID,
                _itemsSchema.VendorDetails.ServiceCommands.NewRow,
                _itemsSchema.VendorDetails.VendorID,
                _itemsSchema.VendorDetails.VendorInventoryID,
                _itemsSchema.VendorDetails.ServiceCommands.NewRow,
                _itemsSchema.VendorDetails.VendorID,
                _itemsSchema.VendorDetails.VendorInventoryID,
                _itemsSchema.CrossReference.AlternateID,
                _itemsSchema.CrossReference.Description,

                _itemsSchema.Actions.Save
            };

            string[][] data = new string[items.Count][];

            int count = 0;
            foreach(Item item in items)
            {
                data[count] = new string[11];
                data[count][0] = item.InventoryID;
                data[count][1] = item.Description.Trim();
                data[count][2] = item.ItemClassID;
                data[count][3] = item.DigiKey;
                data[count][4] = item.DKPN;
                data[count][5] = item.Mouser;
                data[count][6] = item.MouserID;
                data[count][7] = item.Element14;
                data[count][8] = item.Element14ID;
                data[count][9] = item.AlternateID;
                data[count][10] = item.Descr;
                count++;
            }

            _itemsScreen.Import(commands, null, data, false, true, true);

            Console.WriteLine("[{0}] Submitted {1} items to Acumatica.", System.Threading.Thread.CurrentThread.ManagedThreadId, items.Count);
        }
    }
}

我尝试使用FileStream,但没有成功。

【问题讨论】:

    标签: c# image api import acumatica


    【解决方案1】:

    如果 URL 链接是指外部 http 资源,则可以下载图像并上传。

    StockItems 图像字段按显示顺序循环显示文件弹出窗口中包含的所有图像:

    我使用以下代码从静态外部 URL 上传了图片:

        const string imageUrl = "https://cdn.acumatica.com/media/2016/03/software-technology-industries-small.jpg";
        string path = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetTempFileName(), ".jpg"));
    
        // Download Image
        using (WebClient client = new WebClient())
        {
            client.DownloadFile(new Uri(imageUrl), path);
        }
    
        // ReadUploadFile function below
        byte[] data = ReadUploadFile(path);
    
        _itemsScreen.Import(new IN202500.Command[]
        {
            // Get Inventory Item
            new Value
            {
                Value = "D1",
                LinkedCommand = _itemsSchema.StockItemSummary.InventoryID,
            },
            _itemsSchema.Actions.Save,
            // Upload Inventory Item Image
            new Value
            {
                FieldName = Path.GetFileName(path),
                LinkedCommand = _itemsSchema.StockItemSummary.ServiceCommands.Attachment
            },
            _itemsSchema.Actions.Save
        },
        null,
        new string[][]
        {
            new string[]
            {
                // Image data
                Convert.ToBase64String(data)
            }
        },
        false,
        false,
        true);
    
    public byte[] ReadUploadFile(string filePath)
    {
        byte[] filedata;
    
        using (FileStream file = File.Open(filePath,
                                            FileMode.Open,
                                            FileAccess.ReadWrite,
                                            FileShare.ReadWrite))
        {
            filedata = new byte[file.Length];
            file.Read(filedata, 0, filedata.Length);
        }
    
        if (filedata == null || filedata.Length == 0)
        {
            throw new Exception(string.Concat("Invalid or empty file: ", filePath));
        }
    
        return filedata;
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用下面的测试代码。

      
      var content = _context.CR306000GetSchema(); _context.CR306000Clear();
      
      var commands = new List();
      
      ReqParameter(content, ref commands);
      
      commands.Add(content.Actions.Save);
      
      commands.Add(content.CaseSummary.CaseID);
      
      var orderResults = _context.CR306000Submit(commands.ToArray());
      
      private static void ReqParameter(CR306000Content content, ref List cmds) { if (cmds == null) throw new ArgumentNullException("cmds");
      
      
       private static void ReqParameter(CR306000Content content, ref List<Command> cmds)
              {
                  if (cmds == null) throw new ArgumentNullException("cmds");        
      
                  byte[] filedata= null;            
                  Uri uri = new Uri("https://cdn.acumatica.com/media/2016/03/software-technology-industries-small.jpg");  // change the required url of the data that has to be fetched 
      
                  if (uri.IsFile)
                  {
                      string filename = System.IO.Path.GetFileName(uri.LocalPath);
                      filedata = System.Text.Encoding.UTF8.GetBytes(uri.LocalPath);
                  }
                  if (filedata == null)
                  {
                      WebClient wc = new WebClient();
                      filedata = wc.DownloadData(uri);
                  }
      
                  cmds = new List<Command>
                  {
      
                      //Case Header Details
                      new Value { Value="<NEW>",LinkedCommand = content.CaseSummary.CaseID},
                      new Value { Value="L41",LinkedCommand = content.CaseSummary.ClassID},
                      new Value { Value="ABCSTUDIOS",LinkedCommand = content.CaseSummary.BusinessAccount, Commit = true},          
                      new Value { Value="Test subject created from envelop call 11C",LinkedCommand = content.CaseSummary.Subject},           
      
                      // body of the case 
                      new Value{Value= "Body of the content for created through envelop call 11B", LinkedCommand = content.Details.Description},
      
      
                      //Attaching a file
                      new Value
                      {
                          Value = Convert.ToBase64String(filedata),   // byte data that is passed to through envelop 
                          FieldName = "Test.jpg",
                          LinkedCommand =
                      content.CaseSummary.ServiceCommands.Attachment
                      },
      
      
                   };
              }
      

      让我知道这是否适合您。 谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-11
        • 1970-01-01
        • 2017-10-28
        相关资源
        最近更新 更多