允许用户向网站添加图片

时间:2019-04-27 18:12:12

标签: c# asp.net-mvc

我有一个现有的网站,该网站允许用户(通常是我)上传图片,然后在适当的页面上显示图片。我现在正在尝试将此功能添加到新的网站。

在Visual Studio中,我打开了新网站,并做了一个“添加现有项目”以从旧网站复制模型/视图/控制器。进行了一些更改,从代码中删除了不需要的功能,以及其他一些小事情。 在这两种方式中,我都将图像存储在名为/ data / Images的文件夹中。当我为新站点(Lat34North)执行代码并添加图像时,将添加该图像,并且一切似乎都可以正常工作(没有错误)。

问题:

  1. 如果我使用“文件资源管理器”查看文件夹(新解决方案),则在这种情况下,jpg将出现在硬盘驱动器和正确的文件夹中。

  2. 如果我尝试通过Visual Studio访问该文件夹(〜/ Data / Images),则jpg不会显示。

  3. 当我尝试从网站上显示图像时,我只是得到了一个有趣的小图标,当找不到图像时,您会看到它。

照片控制器

//

    [Authorize]
    public ActionResult PhotoCreate(string CallingState, string masterMarkerID, string markerTitle)
    {
        ViewBag.photoCallingState = CallingState;
        ViewBag.photoMarkerID = masterMarkerID;
        ViewBag.photomarkerTitle = markerTitle;
        return View();
    }


    //
    // POST: /Photo/Create

    [AcceptVerbs(HttpVerbs.Post)]
    [Authorize]

    public ActionResult PhotoCreate(Photo photo, HttpPostedFileBase file)
    {

        if (ModelState.IsValid)
        {

            photo.PhotoLinkRecID = photo.savePhotoState + photo.saveMarkerID;
            if (photo.PhotoSequence == 0)
            {


                var no_Photo = from s in db.Photo
                               where s.PhotoLinkRecID == photo.PhotoLinkRecID
                               select s;
                int noPhoto = no_Photo.Count();
                int Sequence = (noPhoto * 20);
                photo.PhotoSequence = Sequence;
            }

            photo.PhotoDateCreated = DateTime.Now;
            photo.PhotoCreatedBy = @User.Identity.Name;

            if (photo.fileUpload != null && photo.fileUpload.ContentLength > 0)
            {


                // get the file extension

                photo.PhotoLinkRecID = photo.savePhotoState + photo.saveMarkerID; // Create key to link photo to the approiate marker -- // unique record identifier
                photo.PhotoState = photo.savePhotoState;
                photo.PhotoMarkerID = photo.saveMarkerID;

                var fileName = Path.GetFileName(photo.fileUpload.FileName);
                var FileExtension = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();
                photo.PhotoFileType = FileExtension;
                db.Photo.Add(photo);  // add new photo record to the DB
                db.SaveChanges();



                photo.PhotoRecID = "Photo" + photo.PhotoID.ToString(); // unique record identifier


                var saveFile = photo.savePhotoState + photo.PhotoRecID + "." + photo.PhotoFileType;           // new file name
                var path = Path.Combine(Server.MapPath("~/Data/Images"), saveFile);     // save the file

                HttpPostedFileBase hpf = photo.fileUpload as HttpPostedFileBase;
                hpf.SaveAs(path);

                Image image = Image.FromFile(path);
                photo.PhotoHeight = image.Height;
                photo.PhotoWidth = image.Width;
                try
                {

                    //get the date taken from the files metadata
                    PropertyItem pi = image.GetPropertyItem(0x9003);
                    string sdate = Encoding.UTF8.GetString(pi.Value).Replace("\0", String.Empty).Trim();
                    string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
                    string firsthalf = sdate.Substring(0, 10);
                    firsthalf = firsthalf.Replace(":", "/");
                    sdate = firsthalf + secondhalf;

                    photo.PhotoDateTaken = sdate;
                }
            catch { }


            try
            {

                double? lat = ImageExtensions.GetLatitude(image);
                double? lon = ImageExtensions.GetLongitude(image);

                if (lat > 1)
                {
                    photo.PhotoLatDirection = "N";
                    string latString = lat.ToString();
                    //photo.PhotoLat = latString.Substring(0, 10);
                    if (latString.Length < 10)
                    {
                        photo.PhotoLat = latString;
                    }
                    else
                    {
                        photo.PhotoLat = latString.Substring(0, 10);
                    }

                    //photo.PhotoLat  = lat.ToString();
                    photo.PhotoLongDirection = "W";
                    string longString = lon.ToString();
                    if (longString.Length < 10)
                    {
                        photo.PhotoLong = longString;
                    }
                    else
                    {
                        photo.PhotoLong = longString.Substring(0, 10);
                    }


                    //photo.PhotoLong = lon.ToString();
                }


            }
            catch { }



            }   
                db.SaveChanges();
                return RedirectToAction("PhotoEdit", new { id=photo.PhotoID });  
        }

            return View(photo);
  • 旧网站使用EntityFramework,版本= 5.0.0.0
  • 新网站使用EntityFramework,版本= 6.0.0.0

两者之间的另一个区别是我已经安装了软件包。我会想念一个吗?

0 个答案:

没有答案