<input type="button" id="btn_QrCode" class="bc-btn btn-right" value="生成二维码" />
<script type="text/javascript"> var QrWin = $("#QrCodeWindows").window({ title: "二维码扫描", width: 200, height: 200, bodyPadding: 0, onClosing: function () { QrWin.window("close"); } }); $("#btn_QrCode").click(function () { var SFSH = $("#hid_SFSH").val() if (SFSH == "0") { $.alert({ msg: "Please select the audited data to generate a QR code!", icon: "info" }); } var DH = $("#txt_DH_A").val(); $("#QRCode").text(""); $.ajax({ type: "POST", dataType: "json", url: "data/PTHC_CGDD.ashx", data: { type: "CGDD_QrCode", DH: DH, times: Math.random() }, success: function (data) { if (data.data == "1") { var divQr = "<div class=\"butright\"><img src=\"" + data.url + "\" style=\" width:200px; height:200px; margin:0 auto;\" /></div></div>"; $("#QRCode").append(divQr); QrWin.window("show"); } else { $.alert({ msg: "Build failed!", icon: "info" }); } } }); }); <script>
public void CGDD_QrCode(HttpContext context) { string DH = context.Request["DH"]; DataSet ds = bll.GetCGDD_Detail(DH); //SBMCBH VARCHAR(20) consumable number //SBMC VARCHAR(80) consumable name //GGXH VARCHAR(100) Specification model //SL FLOAT purchase quantity //YYMC VARCHAR(100) hospital name // { // doc: {dh: 201805020003, rq: 2018 - 05 - 02 00:00:00}, //dtl:[ //1090086 | 5ml Syringe (Supply Room) | 100 Pays / Box | Manufacturer ID | Manufacturer Name | 1 | Box | Remarks, //1090085 | 2.5ml Syringe (Supply Room) | 150 Pays / Box | Manufacturer ID | Manufacturer Name | 1 | Box |, //1090084 | 1ml Syringe (Supply Room) | 200 Pays / Box | Manufacturer ID | Manufacturer Name | 1 | Box |, //1090060 | 3M tape indication (card/ strip/ sticker) || manufacturer id | manufacturer name | 1 | roll|, //107004 | 63D type titanium mesh | 50 * 70mm | Manufacturer id | Manufacturer name | 1 | Piece | //] //} string data = string.Empty; data += "{doc:{dh:"+ ds.Tables[0].Rows[0]["DH"].ToString().Trim() + ",rq:" + Convert.ToDateTime(ds.Tables[0].Rows[0]["RQ"]).ToString("yyyy-MM-dd hh:mm:ss") + "},dtl:["; foreach (DataRow dr in ds.Tables[1].Rows) { data += dr["SBMCBH"].ToString().Trim() + "|" + dr["SBMC"].ToString().Trim() + "|" + dr["SBGG"].ToString().Trim() + "|" + dr["SCQY"].ToString().Trim() + "|" + dr["SCQYMC"].ToString().Trim() + "|" + dr["SL"].ToString().Trim() + "|" + dr["SBDW"].ToString().Trim() + "|" + dr["BZ"].ToString().Trim() + ","; } data=data.Substring(0, data.Length - 1); data += "]}"; string imageUrl = GetQRCode(QRCodeEncoderUtil(data)); context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { url = imageUrl,data="1" })); }
/// 生成二维码
/// </summary>
/// <param name="QRCode"></param>
/// <returns></returns>
public string GetQRCode(Bitmap QRCode)
{
if (!Directory.Exists(System.Web.HttpContext.Current.Request.MapPath("~/QRimages/")))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Request.MapPath("~/QRimages/"));
}
else
{
foreach (string s in Directory.GetFileSystemEntries(System.Web.HttpContext.Current.Request.MapPath("~/QRimages/")))
{
if (File.Exists(s))
{
FileInfo file = new FileInfo(s);
if (file.Attributes.ToString().IndexOf("ReadOnly") != -1)
file.Attributes = FileAttributes.Normal;
File.Delete(s);
}
}
}
string fileName = DateTime.Now.ToFileTime().ToString() + ".jpg";
QRCode.Save(System.Web.HttpContext.Current.Request.MapPath("~/QRimages/") + fileName);
fileName = "../QRimages/" + fileName;
return fileName;
}
public Bitmap QRCodeEncoderUtil(string qrCodeContent)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeVersion = 0;
Bitmap img = qrCodeEncoder.Encode(qrCodeContent, Encoding.UTF8);//指定utf-8编码, 支持中文
return img;
}