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:
thank you