如何使用asp.net mvc 5在数据库中的文件夹和路径上传图像

时间:2017-02-11 09:46:24

标签: razor

大家好我想将数据和图像路径保存到数据库sql server并将图像保存在asp.net MVC 5的文件夹中。我在互联网上搜索了很多页但没有运气。请任何人都可以告诉我如何创建控制器吗?

我的内容

public class Inquiry
{
[Key]
public int InquiryID { get; set; }
public DateTime InquirDate { get; set; }
public string SignBy { get; set; }
public string Description { get; set; }
public int ParentInqNo { get; set; }
public string Status { get; set; }
public int Number { get; set; }
public string Subject { get; set; }
public string EmpId { get; set; }
public string Attachment { get; set; }
public string InqiurURL { get; set; }
public DateTime EnterDate { get; set; }
}

我的观点

<form action="Create" method="post">
    <table>
        <tr>
            <td>
                <input type="date" name="InquirDate" id="InquirDate" placeholder="Date" />
            </td>
            <td>
                <input type="text" id="signby" name="signby" placeholder="signby" />
            </td>
            <td>
                <input type="text" id="description" name="description" placeholder="Description" />
            </td>
            <td>
                <input type="text" id="parentInqNo" name="parentInqNo" placeholder="Parent Inquiry number">
            </td>
            <td>
                <select id="status" name="status">
                    <option>Draft</option>
                    <option>Published</option>
                </select>
            </td>
            <td>
                <input type="text" id="number" name="number" placeholder="number" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="subject" id="subject" placeholder="Subject" />
            </td>
            <td>
                <input type="text" id="empid" name="empid" placeholder="empid" />
            </td>
            <td>
                <input type="file" name="Attachment" id="Attachment" />
            </td>
            <td>
                <input type="text" name="InqiurURL" id="InqiurURL" placeholder="InqiurURL" />
            </td>
            <td>
                <input type="text" name="EnterDate" id="EnterDate" placeholder="EnterDate" />
            </td>
        </tr>
    </table>
    <input type="submit" value="Save">
</form>

1 个答案:

答案 0 :(得分:1)

尝试在控制器中遵循此代码

public ActionResult Save(FormCollection fc, HttpPostedFileBase Attachment)//Instead of Attachment better to use "file"   
    {  
            if (Attachment!= null)
            {
              Database1Entities db = new Database1Entities();
              string ImageName = System.IO.Path.GetFileName(Attachment.FileName);
              string physicalPath =Server.MapPath("~/images/"+ ImageName);

              // save image in folder
              Attachment.SaveAs(physicalPath);

              //save new record in database
               tblA newRecord = new tblA();
               newRecord.empid= fc.empid;
               newRecord.subject= fc.subject;
               //....Add all field which need to save in db
               newRecord.Attachment= ImageName;
               db.tblA.Add(newRecord);
               db.SaveChanges();

            }
            return View();  
      }