最近在學(xué)習(xí)C#編程享甸,做了個(gè)圖書管理系統(tǒng)的小demo箩绍,有個(gè)需求就是可以更加快速地將圖書入庫而不需要手動(dòng)輸入孔庭。因?yàn)閯倓傞_始學(xué)不熟悉 ,最后功能實(shí)現(xiàn)了材蛛,實(shí)現(xiàn)過程可能不規(guī)范圆到。
效果圖:
下面開始說步驟:
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>
//將后臺(tái)處理的數(shù)據(jù)顯示在前端界面,并可以傳回?cái)?shù)據(jù)庫
<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里對應(yīng)方法代碼
這里使用了Ding.QRCode.ZXing類庫將圖書的條形碼解析得到圖書的ISBN號(hào)卑吭,之后借助網(wǎng)上找的isbn接口獲取圖書信息芽淡。
[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);
//創(chuàng)建Web訪問對 象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//通過Web訪問對象獲取響應(yīng)內(nèi)容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//通過響應(yīng)內(nèi)容流創(chuàng)建StreamReader對象,因?yàn)镾treamReader更高級(jí)更快
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
//string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有編碼問題就用這個(gè)方法
string returnXml = reader.ReadToEnd();//利用StreamReader就可以從響應(yīng)內(nèi)容從頭讀到尾
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);
}