将数据库和映像上的映像路径保存到文件夹asp .net mvc中

时间:2017-03-10 17:30:45

标签: c# asp.net-mvc image

我在将图像路径上传到数据库并将imagesg移动到文件夹时遇到问题,这里是代码:

public ActionResult UploadImages(
            HttpPostedFileBase formFilled,
            HttpPostedFileBase sixPics,
            HttpPostedFileBase buyerCNIC,
            HttpPostedFileBase sellerCNIC
            )
        {
            if (Session["user"] == null)
            {
                return RedirectToAction("Login", "User");
            }

            CustomerDocumentImages dbCustomerDocs = new CustomerDocumentImages();

            string recieverName = Request.Form["RecieverName"];
            var allowedExtensions = new[] {
            ".Jpg", ".png", ".jpg", "jpeg"
            };

            dbCustomerDocs.FormFilledPhysicalPath = formFilled.ToString();

            var _formFilled = Path.GetFileName(formFilled.FileName); //getting only file name(a.jpg)  

            var ext = Path.GetExtension(formFilled.FileName);

            if (allowedExtensions.Contains(ext))
            {
                string name = Path.GetFileNameWithoutExtension(_formFilled); //getting file name without extension  
                string formImage = name + "_" + DateTime.Now + ext; //appending the name with id  
                                                           // store the file inside ~/project folder(Img)  
                var path = Path.Combine(Server.MapPath("~/Images"), formImage);
                dbCustomerDocs.FormFilledPhysicalPath = path;
                dbCustomerDocs.FormFilled = _formFilled;

                dbCustomerDocs.RecieverName = recieverName;

                try
                {

                    var currentAllotmentId = Convert.ToInt32(Request.Form["AllotmentId"]);
                    var dbAllotment = db.Allotments.SingleOrDefault(i => i.AllotmentId == currentAllotmentId);
                    dbCustomerDocs.AllotmentId = dbAllotment.AllotmentId;
                    dbAllotment.IsCustomerDocsUploaded = true;

                }
                catch (Exception)
                {
                    ModelState.AddModelError("", "Please select valid allotment");
                }



                db.CustomerDocumentImagess.Add(dbCustomerDocs);
                db.SaveChanges();
                formFilled.SaveAs("~Images"+path);

                return RedirectToAction("Index");

            }
            else
            {
                ViewBag.message = "Please choose only Image file";
            }


            return View();
            }

我也使用了server.mappath,但它在localhost上很好,但在windows live服务器上提供错误(在保存为方法时),如果是combine.mappath工作正常但没有将图像移动到文件夹中。请指导我,提前谢谢。

0 个答案:

没有答案