如何創建環境來選擇圖像文件並將其上傳到asp.net MVC 2?我必須爲此編寫什麼代碼?上傳圖像/文件在asp.net MVC 2
哪個scenariois更好出來:在數據庫
店面形象或在應用領域保持圖像的副本文件系統和維持其在數據庫路徑?什麼代碼必須寫在asp.net MVC 2的首選方案?
如何創建環境來選擇圖像文件並將其上傳到asp.net MVC 2?我必須爲此編寫什麼代碼?上傳圖像/文件在asp.net MVC 2
哪個scenariois更好出來:在數據庫
店面形象或在應用領域保持圖像的副本文件系統和維持其在數據庫路徑?什麼代碼必須寫在asp.net MVC 2的首選方案?
HTML代碼
file to upload: <input type="file" name="Photo" id="Photo" />
C#
public ActionResult Imageupload(MyObject myObject)
{
//PhotoForSingleItem is just a class that has properties
// Name and Alternate text. I use strongly typed Views and Actions
// because I'm not a fan of using string to get the posted data from the
// FormCollection. That just seems ugly and unreliable to me.
//PhotoViewImage is just a Entity framework class that has
// String Name, String AlternateText, Byte[] ActualImage,
// and String ContentType
HttpPostedFileBase file = Request.Files["Photo"];
//newImage.Name = photo.Name;
// newImage.Alt = photo.AlternateText;
//Here's where the ContentType column comes in handy. By saving
// this to the database, it makes it infinitely easier to get it back
// later when trying to show the image.
//patient.photo = file.ContentType;
Int32 length = file.ContentLength;
//This may seem odd, but the fun part is that if
// I didn't have a temp image to read into, I would
// get memory issues for some reason. Something to do
// with reading straight into the object's ActualImage property.
try
{
if (length != 0)
{
if ((file.ContentType == "image/pjpeg") || (file.ContentType == "image/gif") || (file.ContentType == "image/x-png"))
{
byte[] tempImage = new byte[length];
file.InputStream.Read(tempImage, 0, length);
localRepository.SaveOrUpdate(myObject);// you can insert through sql commands.
}
catch(Exception ex){
}
return View()
}
,你可以嘗試此鏈接
http://weblogs.asp.net/imranbaloch/archive/2010/04/03/image-preview-in-asp-net-mvc.aspx
已經示例項目連接。如果你有大量的圖片,你可以存儲在文件系統中。我認爲這是取決於你的情況你的決定
這裏是在數據庫中存儲文件的幾乎零代碼解決方案
http://sfvt.wordpress.com/2010/09/07/asp-net-tutorial-detailsview-insert-imag-binary-database/
享受