sql-server - 将多个图像上传到 SQL 数据库 -
问题描述
我正在尝试将多个图像上传到 SQL 数据库。1 张图片工作正常,但不确定如何制作多张图片。我的数据库有 2 个 varbinary images 列,Image
并且Image2
.
我在发帖时收到一条消息:
输入不是有效的 Base-64 字符串,因为它包含非 base-64 字符、两个以上的填充字符或填充字符中的非法字符。
这是我正在尝试的:
控制器:
[HttpPost]
public ActionResult Create(SwapShop model, HttpPostedFileBase image1, HttpPostedFileBase image2)
{
if (image1 != null)
{
model.Image = new byte[image1.ContentLength];
image1.InputStream.Read(model.Image, 0, image1.ContentLength);
}
if (image2 != null)
{
model.Image2 = new byte[image2.ContentLength];
image2.InputStream.Read(model.Image2, 0, image2.ContentLength);
}
//if (image3 != null)
//{
// model.Image3 = new byte[image3.ContentLength];
// image3.InputStream.Read(model.Image3, 0, image3.ContentLength);
//}
db.SwapShops.Add(model);
db.SaveChanges();
return RedirectToAction("Index");
}
和Create
观点:
@model Intranet.Models.Swap
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!-- jQuery UI CSS Reference -->
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/cssjqryUi")
<script type="text/javascript">
$(function () {
$(".date-picker").datepicker({
dateFormat: 'mm/dd/yy',
buttonText: "<i class='fa fa-calendar'></i>"
});
});
</script>
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Swap", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Swap Shop</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Item, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Item, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Item, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ContactInfo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ContactInfo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ContactInfo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Seller, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Seller, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Seller, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ExpireDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ExpireDate, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.ExpireDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" id="image1" name="image1" />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Image2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" id="image2" name="image2" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
解决方案
在 sql server 上存储图片是一个非常麻烦的过程。所以我建议你。保留文件的路径。这既便宜又容易。我正在添加必要的示例来执行此操作。
- https://www.c-sharpcorner.com/article/asp-net-mvc5-upload-imagefile-into-database/
- https://www.youtube.com/watch?v=5L5W-AE-sEs
或者,您可以在插件的帮助下导入多个图像。Dropzone.js
我希望我知道你是解决方案。有问题再写。
推荐阅读
- node.js - Node JS请求获取原始url
- simulink - 如何将结构用作 simulink 子系统的参数并将内部的字段用作模块参数?
- c++ - 创建一个模板函数,根据模板参数类型条件返回不同的元组类型
- javascript - Vue 中的 STLLoader 无法正常工作
- python - ValueError:使用 sklearn 的 train_test_split 时
- c# - 如何使用 C#“查询”Active Directory
- r - 将数据作为数据框读入,将某些分类变量作为因子,并对某些数值变量进行转换
- python - 无法通过 apache/httpd 从外部 IP 地址访问第二个烧瓶服务器
- python - Heroku python如何使用pip
- firefox - SSO 登录: Cookie 方案在 FireFox 中不匹配