Uploading images via the Magento SOAP API

后端 未结 2 1451
慢半拍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: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");
            }
    

提交回复
热议问题