且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Magento的SOAP API上传图片

更新时间:2022-10-29 19:41:28

这花了我天工作了....这是如何做到这一点。

 公共无效UploadProductImage(字符串SKU,字符串路径)
{
变种的ImageStream =新的MemoryStream();使用

(VAR I = Image.FromFile(@C:\ProductImages\+路径))
{
i.Save(的ImageStream,ImageFormat.Jpeg) ;
}
字节[] = encbuff imageStream.ToArray();

串ENC = Convert.ToBase64String(encbuff,0,encbuff.Length);


变种imageEntity =新catalogProductImageFileEntity();
imageEntity.content = ENC;
imageEntity.mime =图像/ JPEG;
imageStream.Close();


变种entityP =新catalogProductAttributeMediaCreateEntity();
entityP.file = imageEntity;
entityP.types =新[] {图像,small_image,缩略图};
entityP.position =0;
entityP.exclude =0;

_m.catalogProductAttributeMediaCreate(MageSessionProvider.GetSession(),SKU,entityP,默认);
Console.WriteLine(图片上传);
}


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 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");

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");
        }