public void CreateQrCode() { string host = Request.Url.Host; var port = Request.Url.Port; string str = "http://" + host + ":" + port + "/Chemical/ShowChemicalDevice?id=" + Request.Form["qrcode"]; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SewagePlantIMS"].ConnectionString); con.Open(); string sqlStr = "select cd_name from dm_chemical_device where id = " + Request.Form["qrcode"] + ";"; SqlCommand cmd = new SqlCommand(sqlStr, con); string cd_name = cmd.ExecuteScalar().ToString(); con.Close(); using (var memoryStream = QRCodeHelper.GetQRCode(str, 10)) { System.Drawing.Image img = Image.FromStream(memoryStream); img = QRCodeHelper.AddTextToImg(img, cd_name, cd_name); System.IO.MemoryStream ms = new System.IO.MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Response.ContentType = "application/octet-stream"; //文件名+文件格式 (这里编码采用的是utf-8) Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(cd_name + ".png", System.Text.Encoding.UTF8)); Response.BinaryWrite(ms.ToArray()); ms.Dispose(); img.Dispose(); } }