62,236
社区成员




[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
var account = storeDB.Accounts.Where(p => p.Email == User.Identity.Name).FirstOrDefault();
product.Account = account;
storeDB.Products.Add(product);
upLoadImg();
product.ProductURL = savepath;
storeDB.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProductCategoryId = new SelectList(storeDB.ProductCategories, "ProductCategoryId", "Name", product.ProductCategoryId);
return View(product);
}
public bool ValidateImg(string imgName)
{
string[] imgType = new string[] { "gif", "jpg", "png", "bmp" };
int i = 0;
bool blean = false;
string message = string.Empty;
//判断是否为Image类型文件
while (i < imgType.Length)
{
if (imgName.Equals(imgType[i].ToString()))
{
blean = true;
break;
}
else if (i == (imgType.Length - 1))
{
break;
}
else
{
i++;
}
}
return blean;
}
public string upLoadImg(string fileName)
{
//上传和返回(保存到数据库中)的路径
string uppath = string.Empty;
string savepath = string.Empty;
if (Request.Files.Count > 0)
{
HttpPostedFileBase imgFile = Request.Files[fileName];
if (imgFile != null)
{
//创建图片新的名称
string nameImg = DateTime.Now.ToString("yyyyMMddHHmmssfff");
//获得上传图片的路径
string strPath = imgFile.FileName;
//获得上传图片的类型(后缀名)
string type = strPath.Substring(strPath.LastIndexOf(".") + 1).ToLower();
if (ValidateImg(type))
{
//拼写数据库保存的相对路径字符串
savepath = "..\\UpImgs\\";
savepath += nameImg + "." + type;
//拼写上传图片的路径
uppath = Server.MapPath("~/UpImgs/");
uppath += nameImg + "." + type;
//上传图片
imgFile.SaveAs(uppath);
}
return savepath;
}
}
return "";
}
这是我刚刚重新找的。。。可是我能不知道怎么在create里面调用uploadimg(),,求教。。。