Vinod Kumar

Vinod Kumar

  • NA
  • 3
  • 2.8k

ASP.NET MVC File Upload

Feb 4 2018 1:28 AM
Hi,
 
i have one mcv website for add produt detail with image. when i try to upload image in my local system it is working fine but after uploaded to webserver it is not working. i hosted in globhost.
this is the error getting
 
Access to the path 'D:\INETPUB\VHOSTS\vinvisinfoco.com\hoylsurgicals.com\Shopimg\9.jpg' is denied.
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.UnauthorizedAccessException: Access to the path 'D:\INETPUB\VHOSTS\vinvisinfoco.com\hoylsurgicals.com\Shopimg\9.jpg' is denied.
 
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
 
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
 
Source Error:
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
 
iam using this code
  1. public ActionResult Add_Product(FormCollection fr, Models.ItemMaster I, HttpPostedFileBase file)  
  2. {  
  3. TempData["count"] = fr.Get("a1");  
  4. string ItemName = fr.Get(2);  
  5. string ItemCode = fr.Get(3);  
  6. string ItemType = fr.Get(4);  
  7. string Brand = fr.Get(5);  
  8. string Description = fr.Get(6);  
  9. string Catid = Request.Form["Category"];  
  10. string filename = Session["Pid"].ToString();  
  11. string extn = file.FileName.Substring(file.FileName.LastIndexOf('.'));  
  12. filename = filename + extn;  
  13. if (ModelState.IsValid)  
  14. {  
  15. if (file == null)  
  16. {  
  17. ModelState.AddModelError("File""Please Upload Your file");  
  18. }  
  19. else if (file.ContentLength > 0)  
  20. {  
  21. int MaxContentLength = 1024 * 1024 * 4; //Size = 4 MB  
  22. string[] AllowedFileExtensions = new string[] { ".jpg"".png" };  
  23. if (!AllowedFileExtensions.Contains  
  24. (file.FileName.Substring(file.FileName.LastIndexOf('.'))))  
  25. {  
  26. ModelState.AddModelError("File""Please file of type: " + string.Join(", ", AllowedFileExtensions));  
  27. }  
  28. else if (file.ContentLength > MaxContentLength)  
  29. {  
  30. ModelState.AddModelError("File""Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");  
  31. }  
  32. else  
  33. {  
  34. var path = Path.Combine(Server.MapPath("~/Shopimg"), filename);  
  35. file.SaveAs(path);  
  36. filename = "/Shopimg/" + filename;  
  37. Hoylsurgicals.Models.ItemMaster Prdct = new Models.ItemMaster();  
  38. if (ModelState.IsValid)  
  39. {  
  40. try  
  41. {  
  42. Prdct.Itemid = Session["Pid"].ToString();  
  43. Prdct.ItemName = ItemName;  
  44. Prdct.ItemCode = ItemCode;  
  45. Prdct.ItemType = ItemType;  
  46. Prdct.Brand = Brand;  
  47. Prdct.Description = Description;  
  48. Prdct.Picture = filename;  
  49. Prdct.CategoryId = Catid;  
  50. //Prdct.remarks = "verified";  
  51. var db = new Hoylsurgicals.Models.Dbclass();  
  52. db.ItemMasters.Add(Prdct);  
  53. db.SaveChanges();  
  54. TempData["msg"] = "registration successfully";  
  55. return RedirectToAction("Productdetail""Admin");  
  56. }  
  57. catch (Exception e)  
  58. {  
  59. Response.Write(e);  
  60. }  
  61. ModelState.Clear();  
  62. ViewBag.Message = "File uploaded successfully. File path : ~/Shopimg/" + filename;  
  63. }  
  64. }  
  65. }  
  66. }  
  67. return RedirectToAction("Productdetail""Admin");  
  68. }  
please help me to solve this problem
 
thanks
vinod kumar

Answers (1)