Uploading images via the Magento SOAP API

后端 未结 2 1446
慢半拍i
慢半拍i 2021-02-03 14:18

I\'m attempting to upload images to a Magento site using the SOAP API with C#.

This is what I have so far, but it isn\'t working, no exceptions are thrown or anything bu

相关标签:
2条回答
  • 2021-02-03 14:36

    It seems that Dan and I have been puzzled in the same issue on the same days, and we get the same solution!

    I'm using XML-RPC and the Magento API. I wrote this code as a part of a bigger class that read the image data from a file and makes it compatible with Magento API.

      internal void readFromFile(string fullImpgPath)
        {
            m_file.content = System.Convert.ToBase64String(System.IO.File.ReadAllBytes(fullImpgPath));
            string ext = System.IO.Path.GetExtension(fullImpgPath).ToLower();
            switch (ext)
            {
                case ".gif":
                    m_file.mime = "image/gif";
                    break;
    
                case ".jpg":
                case ".jpeg":
                    m_file.mime = "image/jpeg";
                    break;
    
                case ".png":
                    m_file.mime = "image/png";
                    break;
    
                case ".bmp":
                    m_file.mime = "image/bmp";
                    break;
    
                case ".tif":
                case ".tiff":
                    m_file.mime = "image/tiff";
                    break;
    
                default:
                    m_file.mime = "application/octet-stream";
                    break;
            }
        }
    

    The very important thing is that the "content" must be of type string and must be obtained from bytes[] through the call of the system function Convert.ToBase64String(...).

    Concerning the MIME type of the image, only "gif", "jpg" and "png" are supported as I discovered looking inside the Magento API code.

    0 讨论(0)
  • 2021-02-03 14:50

    This took me DAYS to work out.... this is how to do it

    public void UploadProductImage(string SKU, string path)
            {
                var imageStream = new MemoryStream();
    
                using (var i = Image.FromFile(@"c:\ProductImages\" + path))   
                {
                           i.Save(imageStream, ImageFormat.Jpeg);
                }
                    byte[] encbuff = imageStream.ToArray(); 
    
                string enc = Convert.ToBase64String(encbuff,0 , encbuff.Length);
    
    
                var imageEntity = new catalogProductImageFileEntity();
                imageEntity.content = enc;
                imageEntity.mime = "image/jpeg";
                imageStream.Close();
    
    
                var entityP = new catalogProductAttributeMediaCreateEntity();
                entityP.file = imageEntity;
                entityP.types = new[] {"image", "small_image", "thumbnail"};
                entityP.position = "0";
                entityP.exclude = "0";
    
                _m.catalogProductAttributeMediaCreate(MageSessionProvider.GetSession(), SKU, entityP, "default");
                Console.WriteLine("Image Uploaded");
            }
    
    0 讨论(0)
提交回复
热议问题