tags:

views:

1714

answers:

2

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 aint working, no exceptions are thrown or anything but when I go and look on the site the image is blank.

Do I need to do the Base64Encode? I only really tried this because this php example does something similar. If I try it without I get a Soap Exception with the error message of "Bad Request".

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");
+4  A: 

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");
        }
Dan
+1  A: 

Hy Dan
Seems that we've been puzzled in the same issue on the same days, and we get the same solution too !!

I'm using xmlprc and the Magento API.
I wrote this code as a part of a bigger class that read the image data from a file and make 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[] trough the call of system function Convert.ToBase64String(...).

For what concern the "mime" type of the image only "gif", "jpg" and "png" are supported as I discover looking inside the Magento API code.

bye
Giuseppe

Giuseppe