1

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();
        }

}
}

My actual db:

model

thank you

4
  • 1
    You should read some tutorials about Entity Framework. Your problem is not really related to uploading images, but you don't know how to perform basic operations in EF. Commented Feb 13, 2011 at 14:37
  • thank you .. i know is something like _db.PropPicture.AddObject(Prop); _db.SaveChanges(); but i don't know how to integrate with this upload .. i'm sure there are people interested in this answer , thanks Commented Feb 13, 2011 at 16:01
  • How to upload an image into the database is a widely covered topic. Are you sure SO does't already have an answer for this? Commented Feb 13, 2011 at 19:54
  • i don't want to upload binary the images in db .. i don't think is a good practice specially if you have big sized images i just want to add the image name in the db. Commented Feb 13, 2011 at 22:11

1 Answer 1

2

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!";


                    }
                }

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. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you you save my day .. i know it was simple but because i'm begginer in the whole .NET topic i didn't found the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.