Upload images through the SOAP Magento API

I am trying to upload images to a Magento site using the SOAP API with C #.

This is what I have so far, but it does not work, no exceptions happen or nothing, except when I go and look at the site, the image is empty.

Do I need to do Base64Encode? I just tried this because this PHP example does something similar. If I try, without getting a SOAP exception with the "Bad Request" error message.

FileStream fs = File.OpenRead(@"c:\1.jpg"); StreamReader sr = new StreamReader(fs); string fileConent = sr.ReadToEnd(); byte[] encbuff = Encoding.UTF8.GetBytes(fileConent); string enc = Convert.ToBase64String(encbuff); var imageEntity = new catalogProductImageFileEntity(); imageEntity.content = enc; imageEntity.mime = "image/jpeg"; sr.Close(); fs.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"); 
+7
c # magento
source share
2 answers

It took me DAYS to work ... here's 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"); } 
+14
source share

It seems that Dan and I were puzzled by the same question on the same days, and we are getting the same solution!

I am using XML-RPC and the Magento API. I wrote this code as part of a larger class that reads image data from a file and makes it compatible with the 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; } } 

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

As for the MIME type, only "gif", "jpg" and "png" are supported since I found that I was looking at the Magento API code.

+2
source share

All Articles