Asp.net mvc ASP.NET MVC5-图像上载到SQL Server 2008R2标准
我对ASP.NET MVC还不太熟悉,我在过去使用过它,但没有达到我计划使用它的程度,也没有达到我正在从事的一些项目的学习程度。我知道这个问题在互联网上被问了很多次,有很多解决方案,所以我会尽量把上传图像和将其存储在SQL Server数据库中的问题具体化 我目前正在使用.NET4.5和MVC5(VS2013)来完成我的所有编码 首先,我发现了一个很棒的教程,它让我能够将图像上传到我的SQL Server 2008数据库: 在我了解了该站点的功能是如何工作的之后,一切都很顺利,但我遇到的问题是,我有多个文本字段,我想将其中的数据包括到SQL Server数据库中,同时还包括一个图像上载功能,该功能是我根据在线找到的教程创建的 我的代码的第一部分是模型(BeerList.cs)Asp.net mvc ASP.NET MVC5-图像上载到SQL Server 2008R2标准,asp.net-mvc,sql-server-2008-r2,image-uploading,asp.net-mvc-5,Asp.net Mvc,Sql Server 2008 R2,Image Uploading,Asp.net Mvc 5,我对ASP.NET MVC还不太熟悉,我在过去使用过它,但没有达到我计划使用它的程度,也没有达到我正在从事的一些项目的学习程度。我知道这个问题在互联网上被问了很多次,有很多解决方案,所以我会尽量把上传图像和将其存储在SQL Server数据库中的问题具体化 我目前正在使用.NET4.5和MVC5(VS2013)来完成我的所有编码 首先,我发现了一个很棒的教程,它让我能够将图像上传到我的SQL Server 2008数据库: 在我了解了该站点的功能是如何工作的之后,一切都很顺利,但我遇到的问题是,
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用System.ComponentModel.DataAnnotations;
命名空间YBPP_Production.Models
{
公营啤酒店
{
公共int ID{get;set;}
[必需(ErrorMessage=“Name is Required”)]
公共字符串名称{get;set;}
[必需(ErrorMessage=“公司是必需的。”)]
公共字符串公司{get;set;}
[必需(ErrorMessage=“类型是必需的。”)]
公共字符串类型{get;set;}
[必需(ErrorMessage=“城市是必需的。”)]
公共字符串City{get;set;}
[必需(ErrorMessage=“状态是必需的。”)]
公共字符串状态{get;set;}
[必需(ErrorMessage=“国家/地区是必需的”)]
公共字符串国家{get;set;}
公共字符串ABV{get;set;}
公共字符串IBU{get;set;}
}
公共类信息:DbContext
{
公共DbSet MoreDB{get;set;}
}
}
这里列出的字符串是我试图拉入数据库的文本字段,我可以这样做,但当我尝试混合使用图像上载功能时,文本将上载或图像将上载,具体取决于我如何进行调用
我的代码的控制器部分(BeerListController.cs)
//获取:/BeerList/Create
公共操作结果创建()
{
返回视图();
}
//POST:/BeerList/Create
//若要防止套印攻击,请启用要绑定到的特定属性,例如
//更多详细信息请参见http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果创建([Bind(Include=“ID,Name,Company,Type,City,State,Country,ABV,IBU”)]BeerList-BeerList)
{
if(ModelState.IsValid)
{
添加(beerlist);
db.SaveChanges();
返回操作(“索引”);
}
返回视图(beerlist);
}
公共操作结果文件上载(HttpPostedFileBase文件)
{
//开始图像上传过程
foreach(请求文件中的字符串上载)
{
如果(!Request.Files[upload].HasFile())继续;
字符串mimeType=Request.Files[upload].ContentType;
Stream fileStream=Request.Files[upload].InputStream;
string fileName=Path.GetFileName(Request.Files[upload].fileName);
int fileLength=Request.Files[upload].ContentLength;
字节[]文件数据=新字节[文件长度];
读取(fileData,0,fileLength);
const string connect=@“服务器=本地主机;数据库=;uid=;pwd=”;
使用(var conn=new SqlConnection(connect))
{
var qry=“插入beerlist(FileContent、mimeType、FileName)值(@FileContent、@mimeType、@FileName)”;
var cmd=新的SqlCommand(qry,conn);
cmd.Parameters.AddWithValue(“@FileContent”,fileData);
cmd.Parameters.AddWithValue(“@MimeType”,MimeType);
cmd.Parameters.AddWithValue(“@FileName”,FileName);
conn.Open();
cmd.ExecuteNonQuery();
}
}
返回视图();
}
代码的视图部分(Create.cshtml)
@model YBPP\u Production.Models.BeerList
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm(null,null,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
比尔利斯特
@Html.ValidationSummary(true)
@LabelFor(model=>model.Name,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.Company,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Company)
@Html.ValidationMessageFor(model=>model.Company)
@LabelFor(model=>model.Type,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Type)
@Html.ValidationMessageFor(model=>model.Type)
@LabelFor(model=>model.City,新{@class=“controllabel col-md-2”})
@EditorFor(model=>model.City)
@Html.ValidationMessageFor(model=>model.City)
@LabelFor(model=>model.State,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.State)
@Html.ValidationMessageFor(model=>model.State)
@LabelFor(model=>model.Country,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Country)
@Html.ValidationMessageFor(model=>model.Country)
@LabelFor(model=>model.ABV,new{@class=“co
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace YBPP_Production.Models
{
public class BeerList
{
public int ID { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Company is required.")]
public string Company { get; set; }
[Required(ErrorMessage = "Type is required.")]
public string Type { get; set; }
[Required(ErrorMessage = "City is required.")]
public string City { get; set; }
[Required(ErrorMessage = "State is required.")]
public string State { get; set; }
[Required(ErrorMessage = "Country is required")]
public string Country { get; set; }
public string ABV { get; set; }
public string IBU { get; set; }
}
public class Info : DbContext
{
public DbSet<DBName> MoreDB { get; set; }
}
}
// GET: /BeerList/Create
public ActionResult Create()
{
return View();
}
// POST: /BeerList/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,Company,Type,City,State,Country,ABV,IBU")] BeerList beerlist)
{
if (ModelState.IsValid)
{
db.BeerListDB.Add(beerlist);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(beerlist);
}
public ActionResult FileUpload(HttpPostedFileBase file)
{
//Begin the Image Uploading Process
foreach (string upload in Request.Files)
{
if (!Request.Files[upload].HasFile()) continue;
string mimeType = Request.Files[upload].ContentType;
Stream fileStream = Request.Files[upload].InputStream;
string fileName = Path.GetFileName(Request.Files[upload].FileName);
int fileLength = Request.Files[upload].ContentLength;
byte[] fileData = new byte[fileLength];
fileStream.Read(fileData, 0, fileLength);
const string connect = @"Server=localhost;database=<database-name>;uid=<username-here>;pwd=<there-a-passwordhere>";
using (var conn = new SqlConnection(connect))
{
var qry = "INSERT INTO BeerLists (FileContent, mimeType, FileName) VALUES (@FileContent, @mimeType, @FileName)";
var cmd = new SqlCommand(qry, conn);
cmd.Parameters.AddWithValue("@FileContent", fileData);
cmd.Parameters.AddWithValue("@MimeType", mimeType);
cmd.Parameters.AddWithValue("@FileName", fileName);
conn.Open();
cmd.ExecuteNonQuery();
}
}
return View();
}
@model YBPP_Production.Models.BeerList
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype="multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>BeerList</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Company, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Company)
@Html.ValidationMessageFor(model => model.Company)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Type, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Type)
@Html.ValidationMessageFor(model => model.Type)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.State, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.State)
@Html.ValidationMessageFor(model => model.State)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Country, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Country)
@Html.ValidationMessageFor(model => model.Country)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ABV, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ABV)
@Html.ValidationMessageFor(model => model.ABV)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IBU, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.IBU)
@Html.ValidationMessageFor(model => model.IBU)
</div>
</div>
<div class="form-group">
<p class="control-label col-md-2">Image Upload:</p>
<div class="col-md-10">
<input type="file" id="ImageUpload" name="ImageUpload" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" name="Submit" id="Submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
public class BeerListModel
{
public int ID { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required]
public HttpPostedFileBase file { get; set; }
}
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="form-group">
<p class="control-label col-md-2">Image Upload:</p>
<input type="file" id="file" name="file" />
</div>
<div class="form-group">
<input type="submit" name="Submit" id="Submit" value="Create" class="btn btn-default" />
</div>
</div>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(BeerListModel model)
{
if (ModelState.IsValid)
{
//your logic
}
return View("Create");
}