2017-08-07 76 views
0

現在我已成功工作代碼(與多個線程)項目批量導入IN202500屏幕在Acumatica。acumatica進口項目與圖像API

問題是,我正在努力導入項目的圖像,實際上我沒有圖像本身,但只有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但沒有奏效。

回答

0

如果通過URL鏈接你的意思是一個外部的http資源,你可以下載圖像並上傳它。

通過它們顯示包含在順序文件彈出的所有圖像的StockItems像場週期:

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; 
} 
Files Popup

我使用下面的代碼上傳從靜態外部URL的圖像