Best way to insert images programmatically in nop 2.7/2.8?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Hace 11 años
Look at the way pictures are inserted in the Import.  \Libraries\Nop.Services\ExportImport\ImportManager.cs
Hace 11 años
This is some of the code I'm testing.
Stored Proc


Create PROC [dbo].[insertthepictures]  @filepath varchar(1000)
DECLARE @sql nvarchar(MAX)
DECLARE @PictureId INT
DECLARE @ProductId INT

Set @sql = 'INSERT INTO dbo.Picture(PictureBinary, MimeType, SeoFilename, IsNew)
SELECT (SELECT *
FROM OPENROWSET(BULK N' + quotename(@filepath, '''') + ', SINGLE_BLOB) AS IMAGE),
''image/jpeg'', ''null'', ''1'', '

EXEC sp_executesql @sql
        
        
SET @PictureId = (SELECT = Your Choice)
SET @ProductId = (SELECT = Your Choice)
INSERT INTO dbo.Product_Picture_Mapping (ProductId, PictureId, DisplayOrder)
VALUES(@ProductId,@PictureId,'0')

RETURN
end



SET @filepath = 'Drive:\Images\
EXEC dbo.insertthepictures @filepath


This is the basic structure of the code. Its stores as binary but as mentioned I'm Still - testing - store either in Filesystem or Db. With filesystem i have set up a vitual d in IIS as advised.
Hace 11 años
\Libraries\Nop.Services\Installation\InstallationService.cs contains examples of inserting pictures for a product, e.g.


            productRockabillyPolka.ProductPictures.Add(new ProductPicture()
            {
                Picture = pictureService.InsertPicture(File.ReadAllBytes(sampleImagesPath + "product_RockabillyPolka.jpg"), "image/pjpeg", pictureService.GetPictureSeName(productRockabillyPolka.Name), true),
                DisplayOrder = 1,
            });
            _productRepository.Insert(productRockabillyPolka);


...as does \Libraries\Nop.Services\ExportImport\ImportManager.cs mentioned above:


                        productVariant.Product.ProductPictures.Add(new ProductPicture()
                        {
                            Picture = _pictureService.InsertPicture(File.ReadAllBytes(picture), "image/jpeg", _pictureService.GetPictureSeName(name), true),
                            DisplayOrder = 1,
                        });
                        _productService.UpdateProduct(productVariant.Product);


In both examples the MIME type has been hard coded. Does anyone know a good way to determine this dynamically?

It seems it is not as simple as checking the file extension, because a .jpg could be either "image/jpeg" or "image/pjpeg" (progressive jpeg).
Hace 11 años
http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature

The comments are claiming that this code is a better implementation of it: http://www.pinvoke.net/default.aspx/urlmon.findmimefromdata
Hace 11 años
I'm not keen on storing my images in the database... I'll use the file system method where possible.
Hace 11 años
Any further updates on this topic
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.