繁体   English   中英

将图像路径上传到数据库时,对象引用未设置为对象的实例吗? [重复]

[英]Object reference not set to an instance of an object when uploading the image path to database? [duplicate]

这个问题已经在这里有了答案:

net-mvc5我想将图像路径保存到数据库并将图像保存到文件夹,并且在运行代码时出现此错误“对象引用未设置为对象的实例”。

我的图库管理员:

[HttpPost]
public ActionResult saveRecord (HttpPostedFileBase file, studentGallery m)
{
    string ImageName = Path.GetFileName(file.FileName);
    string physicalPath = Server.MapPath("~/images/" + ImageName);
    file.SaveAs(physicalPath);
    tbl_Gallery newRecord = new tbl_Gallery();
    newRecord.EventName =m.EventName ;
    newRecord.EventDate = m.EventDate;
    //.......saving picture url......
    newRecord.Event_PhotoPath = physicalPath;
    //Assign for remaining feilds in table in this way.           
    db.tbl_Gallery.Add(newRecord);
    db.SaveChanges();
    return RedirectToAction("Index");
}

idk在您造成麻烦的地方,但这是一段可用于图像上传的代码,它还会验证图像​​文件是否为图像

[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Create(tbl_Gallery tbl_Gallery, HttpPostedFileBase file)
    {
        if (file != null)
        {
            if (!ImageUpload.IsImage(file))
            {
                ModelState.AddModelError("Picture", "Please choose image only");
            }
            else if (file.ContentLength >= 5242880)
            {
                ModelState.AddModelError("Picture", "Please use a image of size smaller then 5 MB");
            }
            else
            {
                Random rd = new Random();
                int rdnum = rd.Next();
                string path = Server.MapPath("~/Templates/Frontend/img/");
                string filename = rdnum + file.FileName;
                string fullpath = Path.Combine(path, filename);
                file.SaveAs(fullpath);
                tbl_Gallery.Picture = rdnum + file.FileName;
            }
        }
        try
        {
            if (ModelState.IsValid)
            {
                db.tbl_Gallery.Add(tbl_Gallery);
                await db.SaveChangesAsync();


                return Json(new { success = true });
            }
            else
            {
                string errorsList = "";
                foreach (var item in ModelState.Values)
                {
                    foreach (var err in item.Errors)
                    {
                        errorsList += "<li>" + err.ErrorMessage + "</li>";
                    }
                }
                errorsList = "<ul>" + errorsList + "</ul>";
                return Json(new { success = false, errors = errorsList });
            }
        }
        catch
        {
            return Json(new { success = false, model = View(tbl_Gallery) });
        }
    }

这是我在前面的代码中使用的类,以验证所选文件是否为图像

public static bool IsImage(HttpPostedFileBase imgup)
            {
                //THIS CONTAINS ALL ALLOWED FORMATS OF THE SELECTED IMAGE
                string[] formats = new string[] { ".jpg", ".png", ".jpeg", ".bmp" };

                //THIS WILL CHECK IF THE IMAGE IS HAVE ALLOWED FORMAT OR NOT
                if (imgup.ContentType.Contains("image")&& formats.Any(item => imgup.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase)))
                {
                    return true;
                }
                return false;
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM