Adding Pictures to products

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hey Guys,

I am sure you have heard this a thousand times, but I have tried searching the forums on this and cannot find anything to help. I am looking for a way to upload and add my pictures to my products, but not have to do them one by one as I will have many hundreds.

I found this post that looked promising, but the original poster never replied with the code/instructions - https://www.nopcommerce.com/boards/t/200/import-products.aspx

I have been editing my database directly to add and configure categories and manufacturers and this works really well. Also, I have been adding my products via an xls import, and this too works really well. I also noticed that even if I want to modify any or all of my product details all I have to do is modify the original xls sheet and re-import. Works really well!

So just automating the pictures now. If I can get this down my store will work a treat. As the guy mentions in the above post, he managed to get the pictures in by naming them the same as the sku + .jpg. I have actually already done this as I thought it would be a good way of identifying what picture belongs to what product if I have to manually add each picture to each product.

So does anyone have any advice on this?

Many thanks in advance...!

Glenn.
13 years ago
you could try something like this (the code is not tested) :

in the ImportManager.cs method ImportProductsFromXls inside this if (productVariant != null)


                            try
                            {
                                Picture picture;
                                    string strPath = HttpContext.Current.Server.MapPath(".") + "\\images\\" + SKU + ".png";

                                    FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);

                                    // Create a byte array of file stream length
                                    byte[] ImageData = new byte[fs.Length];

                                    //Read block of bytes from stream into the byte array
                                    fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));

                                    //Close the File Stream
                                    fs.Close();

                                    picture = PictureManager.InsertPicture(ImageData, "image/pjpeg", true);
                                    if (picture != null)
                                    {
                                    ProductPicture productPicture = ProductManager.InsertProductPicture(product.ProductId, picture.PictureId, 1);
                                     }
                            }
                            catch (Exception)
                            {
                            }


this assumes that you already uploaded your pictures to Administration\Images folder and that your pictures have .png extension.
13 years ago
Thanks for your reply,

I take it I can just replace the png with jpg in the code, as all my pictures are currently jpegs? No biggie if not, I can convert to png.

Thanks...
13 years ago
roger,

sorry for being such a nob at this, but I have no experiencing when it comes to this type of coding.

1. What exactly do I need to do with your code? I have found                    

var productVariant = ProductManager.GetProductVariantBySKU(SKU);
                    if (productVariant != null)

But where do I copy your code to, I am thinking just replace "null" with it?

2. The file that contains this, importManager.cs is not on my website. But I found it in the source files on my computer, but the one with source code. I uploaded the "no source code" files to my server when I installed. Where should I put this file on my server?

3. lastly! There is no Images directory in my Admin folder on my site, should I just create one?

Many thanks...
13 years ago
glennlawr wrote:

1. What exactly do I need to do with your code? I have found                    

var productVariant = ProductManager.GetProductVariantBySKU(SKU);
                    if (productVariant != null)
But where do I copy your code to, I am thinking just replace "null" with it?

Don't replace. You should put the code inside this "if" block. The block starts with  if (productVariant != null){ .... and ends with }. you should put the code just before the closing }

glennlawr wrote:

2. The file that contains this, importManager.cs is not on my website. But I found it in the source files on my computer, but the one with source code. I uploaded the "no source code" files to my server when I installed. Where should I put this file on my server?

You need to compile the application and then upload the Nop.BusinessLogic.dll, Nop.BusinessLogic.pdb and Nop.BusinessLogic.xml located in the bin folder to you bin folder website.

glennlawr wrote:

3. lastly! There is no Images directory in my Admin folder on my site, should I just create one?

You need to create the folder.

Also you can change the part of the code that says png to jpg to suport you files.
13 years ago
when you say "compile the application" I gather this is done in Visual Basic. I am not using this, as I have no knowledge of this application whatsoever. I have created this store just via my web server. Is there another way to sort these files?

Also, I put the code from the ImportManager file here - http://www.test25.net/myimportmanager.html

Highlighted in red is your code. Is this correct?
13 years ago
The "if" block checks for a condition. In this case it is checking if the product already exists in the database. The first part of the if is the case where the product exists, and will update the product with the new values. If you put the code in this first part every time you update a product, it will insert the picture of the product... So you need to put the code in the "else" part of the "if" block... also don't forget the try at the beginning.

And you need visual studio (express is free.)  to compile.

If you are using nop 1.6 or less you can use Microsoft Visual Web Developer 2008 Express Edition

If using 1.7 or grater you need Microsoft Visual Web Developer 2010 Express.

I don't think there are any other alternatives...
13 years ago
so does this mean I need to start from scratch with my website, or can I just download the files from my webserver and compile these in Visual Web Developer?

I am on v1.8

And code like this then - http://www.test25.net/myimportmanager.html
13 years ago
No you don't need to start the site again, just download the nop version with source code and open in visual studio. make the changes, compile, and upload Nop.BusinessLogic.dll, Nop.BusinessLogic.pdb and Nop.BusinessLogic.xml from the bin folder to the bin folder in you site.

And I think you got the code in the right place.
13 years ago
Will do just that then...!

thanks for all your help and I will post my results, if I ever get any that is :o)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.