"伊兰"是我的数据库中的一个表,ilan有一个名为" kapak_foto"的列。
这是我的代码:
[HttpPost]
[ValidateInput(false)]
public ActionResult ilanver(ilan ilan,HttpPostedFileBase kapak_foto)
{
if (kapak_foto != null)
{
string kapakname = Path.GetFileNameWithoutExtension(kapak_foto.FileName)
+ "-" + Guid.NewGuid() + Path.GetExtension(kapak_foto.FileName);
Image orjres = Image.FromStream(kapak_foto.InputStream);
orjres.Save(Server.MapPath("~/Content/images/pics" + kapakname));
ilan dbres = new ilan();
dbres.kapak_foto = "/Content/images/pics" + kapakname;
}
html部分:
@using (Html.BeginForm("ilanver", "ilanver", FormMethod.Post, new { enctype="multipart/form-data" }))
{ <input type="file" name="kapak_foto"/>}
&#13;
答案 0 :(得分:1)
首先;代码是
orjres.Save(Server.MapPath("~/Content/images/pics/" + kapakname));
二;如果您将使用已保存文件的路径,则必须先将文件位置设置为antoher变量才能保存;
var filePath = "/Content/images/pics/" + kapakname;
orjres.Save(Server.MapPath(filePath));
ilan dbres = new ilan();
dbres.kapak_foto = filePath;
// ... the other codes...
db.ilan.add(dbres); // if your databse name defined before as db!
db.SaveChanges();
如果filePath对于保存文件是正确的,则它可用于url。