图像名称另存为MVC中的system.web.httppostedfilewrapper [英] Image name save as system.web.httppostedfilewrapper in MVC

查看:193
本文介绍了图像名称另存为MVC中的system.web.httppostedfilewrapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是Asp.net的新手MVC我想将图像名称存储到数据库中,我的图像存入文件系统一切正常但是图像存储到数据库中的名称就像这样

Hi All,

I am new to Asp.net MVC i want to store the image name into database and my image into file system everything works fine but the name of the image store into database like this "

System.Web.HttpPostedFileWrapper



请提前帮助我摆脱这个问题。 ..



我的尝试:



型号:



"
please help me to get out of this problem thanks in advance ...

What I have tried:

Model:

public class Products
{
    [Required]
    public int P_ID { get; set; }
    [Required]
    public string P_Name { get; set; }
    [Required]
    public string P_Image { get; set; }
    [Required]
    public int P_Price { get; set; }
    [Required]
    public int P_CatedoryID { get; set; }
}





控制器:



Contoller:

public ActionResult CreateProductsByAjaxCall(Products p)
   {
       if (Request.Files.Count > 0)
       {
           var file = Request.Files[0];
           if (file != null && file.ContentLength > 0)
           {
               var fileName = Path.GetFileName(file.FileName);
               var path = Path.Combine(Server.MapPath("~/misc/"), fileName);
               file.SaveAs(path);
               db.CreateProducts(p);

           }

       }
       return RedirectToAction("Index");
   }





我的数据库功能:





My Database Function:

public void CreateProducts(Products product)
    {
        string query = "Insert into Products (P_Name,P_Image,P_Price,P_CatagoryID) Values (@name,@image,@price,@category)";
        SqlConnection con = new SqlConnection(con_string);
        con.Open();
        SqlCommand cmd = new SqlCommand(query,con);
        cmd.Parameters.AddWithValue("@name",product.P_Name);
        cmd.Parameters.AddWithValue("@image", product.P_Image);
        cmd.Parameters.AddWithValue("@price",product.P_Price);
        cmd.Parameters.AddWithValue("@category", product.P_CatedoryID);
        cmd.ExecuteNonQuery();
        con.Close();
    }

推荐答案

尝试

try
var fileName = Path.GetFileName(file.FileName);
              var path = Path.Combine(Server.MapPath("~/misc/"), fileName);
              file.SaveAs(path);
              p.P_Name = fileName;
              db.CreateProducts(p);


这篇关于图像名称另存为MVC中的system.web.httppostedfilewrapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆