<input type="button" id="btn_QrCode" class="bc-btn btn-right" value="生成二维码" />
<script type="text/javascript"> var QrWin = $("#QrCodeWindows").window({ title: "二维码扫描", 200, height: 200, bodyPadding: 0, onClosing: function () { QrWin.window("close"); } }); $("#btn_QrCode").click(function () { var SFSH = $("#hid_SFSH").val() if (SFSH == "0") { $.alert({ msg: "请选择审核后的数据生成二维码!", 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=" 200px; height:200px; margin:0 auto;" /></div></div>"; $("#QRCode").append(divQr); QrWin.window("show"); } else { $.alert({ msg: "生成失败!", icon: "info" }); } } }); }); <script>
public void CGDD_QrCode(HttpContext context) { string DH = context.Request["DH"]; DataSet ds = bll.GetCGDD_Detail(DH); //SBMCBH VARCHAR(20) 耗材编号 //SBMC VARCHAR(80) 耗材名称 //GGXH VARCHAR(100) 规格型号 //SL FLOAT 采购数量 //YYMC VARCHAR(100) 医院名称 // { // doc: { dh: 201805020003,rq: 2018 - 05 - 02 00:00:00}, //dtl:[ //1090086 | 5ml注射器(供应室) | 100付 / 盒 | 厂家id | 厂家名称 | 1 | 盒 | 备注, //1090085 | 2.5ml注射器(供应室) | 150付 / 盒 | 厂家id | 厂家名称 | 1 | 盒 |, //1090084 | 1ml注射器(供应室) | 200付 / 盒 | 厂家id | 厂家名称 | 1 | 盒 |, //1090060 | 3M胶带指示(卡 / 条 / 胶贴) || 厂家id | 厂家名称 | 1 | 卷 |, //107004 | 63D型钛网 | 50 * 70mm | 厂家id | 厂家名称 | 1 | 片 | //] //} 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;
}