最近在学习C#编程,做了个图书管理系统的小demo,有个需求就是可以更加快速地将图书入库而不需要手动输入。因为刚刚开始学不熟悉 ,最后功能实现了,实现过程可能不规范。
效果图:
t1.jpg
t2.jpg
下面开始说步骤:
1.前端代码
//上传条形码
<form name="isbnform" method="post" onSubmit="return check();" >
<div class="row" style="margin-bottom: 20px;margin-left: 185px">
<input type="file" style="width: 500px" name="image" />
<input type="submit" value="上传"/><br />
</div>
</form>
//将后台处理的数据显示在前端界面,并可以传回数据库
<form method="post" enctype="multipart/form-data" asp-controller="Book" asp-action="Create">
<div class="form-group row" style="margin-bottom: 10px">
<label asp-for="BookName" class="col-sm-2" style="margin-top: 20px"></label>
<div class="col-sm-10">
<input class="form-control primary" asp-for="BookName" id="BookName">
<span class="invalid" asp-validation-for="BookName"></span>
</div>
</div>
<div class="form-group row" style="margin-bottom: 10px">
<label asp-for="ISBN" class="col-sm-2" style="margin-top: 20px"></label>
<div class="col-sm-10">
<input class="form-control primary" asp-for="ISBN">
<span class="invalid" asp-validation-for="ISBN"></span>
</div>
</div>
<div class="form-group row" style="margin-bottom: 10px">
<label asp-for="Author" class="col-sm-2" style="margin-top: 20px"></label>
<div class="col-sm-10">
<input class="form-control primary" asp-for="Author">
<span class="invalid" asp-validation-for="Author"></span>
</div>
</div>
<div class="form-group row" style="margin-bottom: 10px">
<label asp-for="Press" class="col-sm-2" style="margin-top: 20px"></label>
<div class="col-sm-10">
<input class="form-control primary" asp-for="Press">
<span class="invalid" asp-validation-for="Press"></span>
</div>
</div>
<div class="form-group row" style="margin-bottom: 10px">
<label asp-for="PressDate" class="col-sm-2" style="margin-top: 20px"></label>
<div class="col-sm-10">
<input class="form-control primary" asp-for="PressDate">
<span class="invalid" asp-validation-for="PressDate"></span>
</div>
</div>
<div class="form-group row" style="margin-bottom: 10px">
<label asp-for="Price" class="col-sm-2" style="margin-top: 20px"></label>
<div class="col-sm-10">
<input class="form-control primary" asp-for="Price">
<span class="invalid" asp-validation-for="Price"></span>
</div>
</div>
<div class="form-group row" style="margin-bottom: 10px">
<label asp-for="BookStock" class="col-sm-2" style="margin-top: 20px"></label>
<div class="col-sm-10">
<input class="form-control primary" asp-for="BookStock">
<span class="invalid" asp-validation-for="BookStock"></span>
</div>
</div>
<div class="form-group" style="margin-bottom: 10px">
<label asp-for="Introduce" class="col-sm-2" style="margin-top: 20px"></label>
<textarea class="form-control" rows="5" asp-for="Introduce"></textarea>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div style="position: relative;">
<label>
Image
<a class="btn btn-primary" href="javascript:;">
选择图片
<input type="file" name="Image" size="40" accept="image/*"
style="position: absolute; z-index: 2; top: 0; left: 0; filter: alpha(opacity=0); opacity: 0; background-color: transparent; color: transparent"
onchange="preview(this)" />
</a>
</label>
<img style="width: 150px;" class="hidden preview img-thumbnail">
</div>
</div>
<div class="invalid" asp-validation-summary="ModelOnly">
</div>
<button type="submit" class="btn btn-primary">提交</button>
<a asp-action="Index" class="btn btn-secondary">返回列表</a>
</form>
2.controller里对应方法代码
这里使用了Ding.QRCode.ZXing类库将图书的条形码解析得到图书的ISBN号,之后借助网上找的isbn接口获取图书信息。
ZXing.jpg
[HttpPost]
public IActionResult SearchISBN(string image)
{
//image = "G:/file/picture/" + image;
Image img = Image.FromFile(image);
Bitmap b = new Bitmap(img);
//该类名称为BarcodeReader,可以读二维码和条形码
var zzb = new ZXing.ZKWeb.BarcodeReader();
zzb.Options = new DecodingOptions
{
CharacterSet = "UTF-8"
};
Result r = zzb.Decode(b);
string resultText = r.Text;
b.Dispose();
img.Dispose();
string serviceUrl = string.Format("{0}/{1}", "http://book.feelyou.top/isbn", resultText);
//创建Web访问对 象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//通过Web访问对象获取响应内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
//string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
reader.Close();
myResponse.Close();
//return returnXml;
BookCreateViewModel book = new BookCreateViewModel();
JObject obj = JObject.Parse(returnXml);
book.BookName = obj["title"].ToString();
book.ISBN = obj["isbn"].ToString();
book.Introduce = obj["book_intro"].ToString();
book.Author= obj["abstract"].ToString().Split('/')[0];
book.Press= obj["abstract"].ToString().Split('/')[1];
book.PressDate = Convert.ToDateTime(obj["abstract"].ToString().Split('/')[2]);
string str = obj["abstract"].ToString().Split('/')[3];
str = Regex.Replace(str, @"[^\d.\d]", "");
if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"))
{
decimal result = decimal.Parse(str);
book.Price = result;
}
return View(book);
}