文件名为​​MVC图片上传存储在数据库 [英] MVC image upload with filename stored in db

查看:107
本文介绍了文件名为​​MVC图片上传存储在数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我会想实现这个code在我的应用程序,但我不知道如何将图像名称加入到数据库中,当一个文件被上传

Hello i will like to implement this code in my application but i don't know how to add the image name into database when a file is uploaded

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using accomm2.Models;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace accomm2.Controllers
{
    public class AdminController : Controller
    {
        dataEntities3 _db = new dataEntities3();

        //fileupload

        public ActionResult UploadImage()
        {
            ViewBag.Message = "Welcome to GeeksShip.com! Example Upload & Resize Images";
            const string folderThumbPath = "/Content/StoredImages/Thumbs/";

            var directoryThumbs = new DirectoryInfo(Server.MapPath(folderThumbPath));

            var listImages = directoryThumbs.GetFiles().Select(file => file.Name).ToList();
            ViewBag.listImages = listImages;

            return View("UploadImage");
        }



        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadImage(string str)
        {
            if (ModelState.IsValid)
            {
                if (Request.Files != null)
                {

                    var posted = Request.Files["uploadFile"];
                    if (posted.FileName != "")
                    {


                        const string pathStoredImage = "/Content/StoredImages/Images/";
                        const string pathStoredThumbs = "/Content/StoredImages/Thumbs/";


                        var imageName = Path.GetFileName(posted.FileName);
                        var filePath = pathStoredImage + imageName;

                        posted.SaveAs(Server.MapPath(filePath));
                        ResizeImage(filePath, 150);

                        ViewBag.message = ViewBag.message + "<br >" +
                        "Đã upload <b>" + posted.FileName + "</b> hình ảnh thành công!";


                    }
                }

            }
            return RedirectToAction("UploadImage");
        }

        public void ResizeImage(string filepath, int imageSize)
        {
            var newImage = new Bitmap(Server.MapPath(filepath));
            int thumbnailSize = imageSize;
            int newWidth, newHeight;

            if (newImage.Width > newImage.Height)
            {
                newWidth = thumbnailSize;
                newHeight = newImage.Height * thumbnailSize / newImage.Width;
            }
            else
            {
                newWidth = newImage.Width * thumbnailSize / newImage.Height;
                newHeight = thumbnailSize;
            }

            var thumbnailBitmap = new Bitmap(newWidth, newHeight);

            var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbnailGraph.DrawImage(newImage, imageRectangle);

            filepath = filepath.Replace("/StoredImages/Images/", "/StoredImages/Thumbs/");
            thumbnailBitmap.Save(Server.MapPath(filepath));
            thumbnailGraph.Dispose();
            thumbnailBitmap.Dispose();
            newImage.Dispose();
        }

}
}

我的实际DB:

感谢您

推荐答案

只需添加专线为您保存图像后立即对数据库插入记录。在code应该是这样的:

Just add the line for inserting the record on the database right after you save the image. The code should be something like this:

if (Request.Files != null)
                {

                    var posted = Request.Files["uploadFile"];
                    if (posted.FileName != "")
                    {


                        const string pathStoredImage = "/Content/StoredImages/Images/";
                        const string pathStoredThumbs = "/Content/StoredImages/Thumbs/";


                        var imageName = Path.GetFileName(posted.FileName);
                        var filePath = pathStoredImage + imageName;

                        posted.SaveAs(Server.MapPath(filePath));
                        ResizeImage(filePath, 150);

                        /**** begin db saving ****/
                        PropPicture prop = new PropPicture();
                        prop.PictureName = imageName;
                        _db.PropPicture.AddObject(prop);
                        _db.SaveChanges();
                        /**** end db saving ****/

                        ViewBag.message = ViewBag.message + "<br >" +
                        "Đã upload <b>" + posted.FileName + "</b> hình ảnh thành công!";


                    }
                }

就仔细检查了DB节约code。我没有在此计算机上的Visual Studio,所以我不能测试,看看它是否是正确的。 :)

Just double check the "db saving" code. I don't have Visual Studio on this computer so I can't test to see if it's correct. :)

这篇关于文件名为​​MVC图片上传存储在数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆