NopcommerceNote

Tuesday, 4 April 2017

Registered User Cannot Upload Pic thru FineUploader - Nopcommerce

April 04, 2017 Posted by Munirah Hazam , No comments
Tonight, im working on this error. Getting this error after i copy, paste and use uploading picture function from administrator folder.

At first, its not showing the error because of i am logging as Administrator, uploading picture passed with flying colors. when i logging as registered user, its start shows the error. ' Failed Upload'. Its look like looking administrator privileges to proceed that action.
.
.
So, here the fastest solution .


Hint  : Create Picture.cshtml and PictureController.cs for Nop.Web(Registered Users).

Those files are located at Administrator Section. You can find Picture.cshtml in Admin/Views/Shared/EditorTemplate folder. and PictureController.cs in Admin/Controller Folder.

First at all, i copy Picture.Controller to Nop.Web Controller folder. then i remove '[AdminAntiForgery(true)] ' at AsyncUpload function. 

the next step, Copy Picture.cshtml and paste it in new folder called EditorTemplate in Views/Shared directory (Nop.Web). This because UHint itself will look in that directory by default. Rename that file to your own definition like CustUploadPicture.cshtml.

In CustUploadPicture file, a bit changes need to do. i added @using Nop.Core.Infrastructure; at the top because it didn't know where Engine was located in var pictureService = EngineContext.Current.Resolve<Nop.Services.Media.IPictureService>();

then, I changed : '@(Url.Content("~/Admin/Picture/AsyncUpload"))' to '@(Url.Content("~/Picture/AsyncUpload"))' to also insure its refering AsyncUpload in  Picture controller (Nop.Web)

as you know, the html code is referring to UHint Attribute to upload an image. this UHint Attribute is declared in  your model declaration (*****Model.cs) file. We need to change it to point at new pointer to avoid its referring Admin function and class. So for this case,i open my model file and  i change Uhint["Picture"] to UHint["CustUploadPicture"] based on cshtml named before.



  [UIHint("CustUploadPicture")]

   [NopResourceDisplayName("Admin.Catalog.Products.Pictures.Fields.Picture")]

   public int PictureId { get; set; }

Rebuild solution. See the result.

Thanks and regards,
Munirah Hazam.