您好我想在我的应用程序中实现此代码,但我不知道如何在上传文件时将图像名称添加到数据库中
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();
}
}
}
我的实际数据库:
谢谢
答案 0 :(得分:2)
保存图像后,只需添加用于在数据库中插入记录的行。代码应该是这样的:
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!";
}
}
只需仔细检查“数据库保存”代码即可。我在这台计算机上没有Visual Studio,所以我无法测试它是否正确。 :)