如何使用mvc 4在数据库中保存上传的文件名? [英] how to save uploaded file name in database using mvc 4?

查看:87
本文介绍了如何使用mvc 4在数据库中保存上传的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

code in business layer :

FilesEntity files = new FilesEntity();
        public void SaveFileName(FilesEntity filentity)
        {
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spnotaryfiles", con);
                cmd.Parameters.AddWithValue("@Filename", files.filename);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }





COntroller中的代码:



Code in COntroller :

[HttpPost]
        public ActionResult Index()
        {
            
            {
                FilesEntity entity = new FilesEntity();
                string path = Path.Combine(Server.MapPath("~/Images"),
                Path.GetFileName(entity.filename));
                
                DataAccess da = new DataAccess();
                da.SaveFileName(entity);
            }
            
            return View();
        }





视图中的COde



COde in view

@using (Html.BeginForm("Index", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <input type="file" name="fileUpload" /><br />
    <input type="submit" name="Submit" id="Submit" value="Upload" />
  
}





请帮我将文件名保存在数据库中。提前谢谢



please help me to save the file name in database. thanks in advance

推荐答案

尝试使用此业务逻辑:

Try out with this business logic:
FilesEntity files = new FilesEntity();
public void SaveFileName(FilesEntity filentity)
{
    files = filentity;
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
    SqlCommand cmd = new SqlCommand("spnotaryfiles", con);
    cmd.Parameters.AddWithValue("@Filename", files.filename);
    cmd.CommandType = CommandType.StoredProcedure;
    con.Open();
    cmd.ExecuteNonQuery();
    }
}





-KR



-KR


这篇关于如何使用mvc 4在数据库中保存上传的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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