使用MVC上传图像 [英] Upload image using MVC
问题描述
当我输入玩家的其他信息时,我想使用MVC为玩家上传图像
I want to upload an image for a player using MVC when I enter other information of a player like
namespace FootBall.Models
{
public class Player
{
public int PlayerID { get; set; }
[Required]
[Display(Name = "Name")]
public string PlayerName { get; set; }
[Display(Name = "Gersy No")]
public string PlayerPosition { get; set; }
[Display(Name = "Details")]
public string PlayerDetails { get; set; }
public int TeamID { get; set; }
}
}
没有上传图片我可以使用控制器上的以下行保存它们/>
without uploading image I can just save them using this following line on controller
[HttpPost]
public ActionResult Create(Player player)
{
if (ModelState.IsValid)
{
context.Players.Add(player);
context.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PossibleTeams = context.Teams;
return View(player);
}
但是应该在此模型和控制器中添加什么来添加图像以及确切的视图代码。
N:B:我读了很多关于这个的文章但是当我要在我的代码中应用它时,我的代码卡住了。
请你帮帮我吗?
提前致谢
But what should be added in this model and controller to add image with that and what would be the exact view code.
N:B: I read many article about this but when I am going to apply that in my code I get stuck by my code.
Would you please help me to do that??
Thanks in advance
推荐答案
FileUpload控件
使用纯HTML输入文件类型标签。
FileUpload Control
Using plain HTML input file type tag.
<input name="Photo" id="Photo" type="file" />
POST DATA
简单按照此流程将数据提交到数据库中
POST DATA
Simple follow this process to submit data into database
Controllers -> public FileContentResult Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
.....
person.Photo = imageToByteArray(collection["Photo"]);
.....
}
public static byte[] imageToByteArray(string sPath)
{
try
{
System.Net.WebClient client = new System.Net.WebClient();
byte[] imageData = client.DownloadData(sPath);
System.IO.MemoryStream stream = new System.IO.MemoryStream(imageData);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
catch (Exception) { return null; }
}
查看图像控件
我再次使用HTML标签显示图像。请参见为数据库保存的byte []和从字节数组中显示的图像。文件夹路径对每个人来说都很简单,这也是。但出于演示目的,我将通过varbinary格式。当我来显示一个人的细节时,我会卡在显示图像上。我做了以下方法:
View Image Control
Again I am using HTML tag to display image. See byte[] saved for database and image to display from a byte array. Where folder path will simple for everyone and this is also. But for demo purposes, I am going through varbinary format. When I come to display the details for a Person, I get stuck on displaying the Image. I did the following method:
Controllers -> public FileContentResult getImg(int id)
public FileContentResult getImg(int id)
{
byte[] byteArray = db.Persons.Find(id).Photo;
if (byteArray != null)
{
return new FileContentResult(byteArray, "image/jpeg");
}
else
{
return null;
}
}
Views -> Design
<img src="@Url.Action(" getimg=", " controls_=", new { id = Model.ID })" alt="Person Image" />
获取更多详情
使用FileUpload [ ^ ]
这篇关于使用MVC上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!