我想在不使用entityframework的情况下将图像上传到MVC中的数据库 [英] I want upload image to database in MVC without using entityframework

查看:74
本文介绍了我想在不使用entityframework的情况下将图像上传到MVC中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这种方式,但我得到了错误请帮助我的朋友....





  public  ActionResult Upload()
{
return View();
}
[HttpPost]
public ActionResult Upload(Image obj,HttpPostedFileBase aFile)
{

MySqlCommand cmd11 = new MySqlCommand( 选择计数(*)来自学生,Con);
Con.Open();
int i = Convert.ToInt32(cmd11.ExecuteScalar())+ 1 ;
GenId = i + DateTime.Now.ToString( ss);


// HttpPostedFileBase aFile = Request.Files [Filedata];
// int contentLength = aFile.ContentLength;
< span class =code-comment> // byte [] bytePic = new byte [contentLength];
// aFile.InputStream.Read(bytePic,0,contentLength);

string filePath = Path.GetFileName(aFile.FileName);
string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,Path.GetFileName(aFile.FileName));
aFile.SaveAs(Server.MapPath( 〜/ Content / Images / + filePath ));

MySqlCommand cmd = new MySqlCommand( 插入学生值(@ Id,@ Name,@ EmailId,@ MobileNo,@ City,@ Image,Con);
cmd.Parameters.AddWithValue( @ Id,GenId);
cmd.Parameters.AddWithValue( @ Name,obj.Name);
cmd.Parameters.AddWithValue( @ EmailId,obj.EmailId);
cmd.Parameters.AddWithValue( @ City,obj.City);
cmd.Parameters.AddWithValue( < span class =code-string> @ MobileNo,obj.MobileNo);
cmd.Parameters.AddWithValue( @ Image,savedFileName);
cmd.ExecuteNonQuery();
return View();
}





我的尝试:



公共ActionResult上传()

{

返回查看();

}

[HttpPost]

公共ActionResult上传(图片obj,HttpPostedFileBase aFile)

{



MySqlCommand cmd11 = new MySqlCommand(从学生中选择计数(*),Con);

Con.Open();

int i = Convert.ToInt32(cmd11.ExecuteScalar()) + 1;

GenId = i + DateTime.Now.ToString(ss);





// HttpPostedFileBase aFile = Request.Files [Filedata];

// int contentLength = aFile.ContentLength;

// byte [] bytePic = new byte [contentLength ];

//aFile.InputStream.Read(bytePic,0,contentLength);



string filePath = Path.GetFileN ame(aFile.FileName);

string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,Path.GetFileName(aFile.FileName));

aFile.SaveAs( Server.MapPath(〜/ Content / Images /+ filePath));



MySqlCommand cmd = new MySqlCommand(插入学生值(@ Id,@ Name) ,@ EmailId,@ MobileNo,@ City,@ Image,Con);

cmd.Parameters.AddWithValue(@ Id,GenId);

cmd.Parameters .AddWithValue(@ Name,obj.Name);

cmd.Parameters.AddWithValue(@ EmailId,obj.EmailId);

cmd.Parameters.AddWithValue (@ City,obj.City);

cmd.Parameters.AddWithValue(@ MobileNo,obj.MobileNo);

cmd.Parameters.AddWithValue( @Image,savedFileName);

cmd.ExecuteNonQuery();

返回View();

}

解决方案

c# - 如何在数据库中保存图像并将其显示到MVC 4中的观点? - 堆栈溢出 [ ^ ]



在数据库MVC中上传图像 [ ^ ]



http://www.dotnetawesome.com/2014/ 05 / how-to-upload-image-to-database-and-show-in-view-without-image-handler.html [ ^ ]

i am trying this way but i got error please Help me Friends....


public ActionResult Upload()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Upload(Image obj, HttpPostedFileBase aFile)
        {

            MySqlCommand cmd11 = new MySqlCommand("Select Count(*) from student", Con);
            Con.Open();
            int i = Convert.ToInt32(cmd11.ExecuteScalar()) + 1;
            GenId = i + DateTime.Now.ToString("ss");


            //HttpPostedFileBase aFile = Request.Files["Filedata"];
            //int contentLength = aFile.ContentLength;
            //byte[] bytePic = new byte[contentLength];
            //aFile.InputStream.Read(bytePic, 0, contentLength);

            string filePath = Path.GetFileName(aFile.FileName);
            string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(aFile.FileName));
            aFile.SaveAs(Server.MapPath("~/Content/Images/" + filePath));

            MySqlCommand cmd = new MySqlCommand("Insert into student values(@Id,@Name,@EmailId,@MobileNo,@City,@Image", Con);
            cmd.Parameters.AddWithValue("@Id", GenId);
            cmd.Parameters.AddWithValue("@Name", obj.Name);
            cmd.Parameters.AddWithValue("@EmailId", obj.EmailId);
            cmd.Parameters.AddWithValue("@City", obj.City);
            cmd.Parameters.AddWithValue("@MobileNo", obj.MobileNo);
            cmd.Parameters.AddWithValue("@Image", savedFileName);
            cmd.ExecuteNonQuery();
            return View();
        }



What I have tried:

public ActionResult Upload()
{
return View();
}
[HttpPost]
public ActionResult Upload(Image obj, HttpPostedFileBase aFile)
{

MySqlCommand cmd11 = new MySqlCommand("Select Count(*) from student", Con);
Con.Open();
int i = Convert.ToInt32(cmd11.ExecuteScalar()) + 1;
GenId = i + DateTime.Now.ToString("ss");


//HttpPostedFileBase aFile = Request.Files["Filedata"];
//int contentLength = aFile.ContentLength;
//byte[] bytePic = new byte[contentLength];
//aFile.InputStream.Read(bytePic, 0, contentLength);

string filePath = Path.GetFileName(aFile.FileName);
string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(aFile.FileName));
aFile.SaveAs(Server.MapPath("~/Content/Images/" + filePath));

MySqlCommand cmd = new MySqlCommand("Insert into student values(@Id,@Name,@EmailId,@MobileNo,@City,@Image", Con);
cmd.Parameters.AddWithValue("@Id", GenId);
cmd.Parameters.AddWithValue("@Name", obj.Name);
cmd.Parameters.AddWithValue("@EmailId", obj.EmailId);
cmd.Parameters.AddWithValue("@City", obj.City);
cmd.Parameters.AddWithValue("@MobileNo", obj.MobileNo);
cmd.Parameters.AddWithValue("@Image", savedFileName);
cmd.ExecuteNonQuery();
return View();
}

解决方案

c# - How to save image in database and display it into Views in MVC 4? - Stack Overflow[^]

Upload Image in Database MVC[^]

http://www.dotnetawesome.com/2014/05/how-to-upload-image-to-database-and-show-in-view-without-image-handler.html[^]


这篇关于我想在不使用entityframework的情况下将图像上传到MVC中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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