Arjun M.R

Arjun M.R

  • NA
  • 190
  • 13.2k

Binding image in jquery datatable using asp.net mvc

Oct 11 2020 9:08 AM
Hello everyone,
 
I tried to bind image inside jquery datatable plugin but it is showing an error 'Not allowed to load local resource: file:///E:/myworks/Demo/Images/Img/16.png' .
 
I am attaching my code along with it. Please help me to get out of this issue.
  1. $('#dataTable').DataTable({  
  2. ordering: true,  
  3. searching: false,  
  4. paging: false,  
  5. autoWidth: true,  
  6. processing: true,  
  7. responsive: true,  
  8. bSort:true,  
  9. language: {  
  10. "processing""Loading..."  
  11. },  
  12. ajax: {  
  13. url: "/Login/GetAllData",  
  14. type: "POST",  
  15. datatype: "json"  
  16. },  
  17. pageLength: 7,  
  18. columns: [  
  19. "data""ID""name":"ID""defaultContent""<i>-</i>" },  
  20. "data""Type","name":"Type""defaultContent""<i>-</i>" },  
  21. "data""To""name""To""defaultContent""<i>-</i>" },  
  22. {  
  23. "data""ImagePath""name""ImagePath""defaultContent""<i>-</i>",  
  24. "render"function (data, type, row, meta) {  
  25. debugger;  
  26. return '<img src="' + data + '"height="70px" width="40px"/>';  
  27. }  
  28. }  
  29. ],  
  30. columnDefs: [  
  31. { className: "text-center""targets": [0, 1, 2] },  
  32. //{  
  33. // "data": "ImagePath",  
  34. // "render": function (data, type, row, meta) {  
  35. // return '<img src="' + data + '" alt="' + data + '"height="16" width="16"/>';  
  36. // }  
  37. //}  
  38. ],  
  39. destroy:true  
  40. })  
and my controller is given below
  1. try  
  2. {  
  3. if (busModelVM.File != null&&busModelVM.File.ContentLength>0)  
  4. {  
  5. var fileName = Path.GetFileName(busModelVM.File.FileName);  
  6. var ext = Path.GetExtension(busModelVM.File.FileName);  
  7. string name = Path.GetFileNameWithoutExtension(fileName);  
  8. string myFile = name + ext;  
  9. var path = Path.Combine(Server.MapPath("/Images/Img"),myFile);  
  10. busModelVM.File.SaveAs(path);  
  11. busModelVM.ImagePath = path;  
  12. //string filename = Path.GetFileName(busModelVM.File.FileName);  
  13. //string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(file.FileName));  
  14. //string imagePath = Path.Combine(Server.MapPath("/Uploads/"), busModelVM.File.FileName);  
  15. //var ext = Path.GetExtension(busModelVM.File.FileName);  
  16. //string name = Path.GetFileNameWithoutExtension(fileName);  
  17. //busModelVM.File.SaveAs(imagePath);  
  18. //busModelVM.ImagePath = imagePath;  
  19. }  
  20. var result = _class1.InsertUpdateData(_mapper.MapToBusMaster(busModelVM), conString);  
  21. //return Json(new { data = result }, JsonRequestBehavior.AllowGet);  
  22. return JsonConvert.SerializeObject(new { Status = "OK", Records = result, Message = "Insertion sucessfull" });  
  23. }  
  24. catch(Exception ex)  
  25. {  
  26. //return Json(new { data = "" }, JsonRequestBehavior.AllowGet);  
  27. return JsonConvert.SerializeObject(new { Status = "ERROR", Records = "", Message = ex });  
  28. }  
I think it is an issue in saving image path. Please help me to get out of the issue.

Answers (4)