ajax Bitmap 批量生成二维码到压缩包

1.前台:

function fnMakeCodesSelect() {
            var aryRow = $(list1).jqGrid('getGridParam', 'selarrrow');
            if (aryRow.length >= 1) {
                var phoneIds = "";
                for (var i = 0; i < aryRow.length; i++) {
                    phoneIds += $(list1).getCell(aryRow[i], "PhoneID") + ",";
                    phoneIds += $(list1).getCell(aryRow[i], "Number") + "|";
                }
                if (phoneIds != "") {
                    phoneIds = phoneIds.substring(0, phoneIds.length - 1);
                }
            }
            else {
                alert("请选择一条记录!");
                return;
            }
            $.ajax({
                type: "POST",
                url: "../../api/Sale/MakeORCodes?aaa=123&phoneids=" + phoneIds,
                data: "",
                success: function (msg) {
                    window.location = "../../../../FileTemp/SaleData/qrcode/二维码.zip";
                }, error: function (ex) {
                    var e = ex;
                }
            });
        }

2.controller

#region 选中二维码
        public void MakeORCodes()
        {
            HttpContext.Current.Response.Write(objSM.MakeORCodes(HttpContext.Current));
            HttpContext.Current.Response.End();
        }
        #endregion

3.业务类代码

#region 批量生成二维码
        /// <summary>
        /// 批量生成二维码
        /// </summary>
        /// <param name="id"></param>
        public string MakeORCodes(System.Web.HttpContext context)
        {
            try
            {
                string aaa = context.Request["aaa"];
                string phoneids = context.Request["phoneids"];
                //var idd = id.Substring(1);
                var list = new List<string>(phoneids.Split('|'));
                if (list != null && list.Any())
                {
                    List<string> urlList = new List<string>();
                    foreach (var item in list)
                    {
                        if (item != null && !string.IsNullOrEmpty(item))
                        {
                            var items = item.Split(',');
                            string urlHtml = "http://192.168.20.158:8012/Site/Sale/PhoneShow.aspx?phoneid="+ items[0];//
                            var url = items[1] + ".png";
                            var url1 = context.Server.MapPath("..\\..\\FileTemp\\SaleData\\qrcode") + "\\" + "bg.jpg";
                            //string urlHtml = string.Format(@"{0}?id={1}", UrlPost, items[1]);//jmId
                            var img = CreateORCode.GenerateQrCodeWithLogo(urlHtml, 400, 400, url1, items[1]);
                            System.IO.MemoryStream MStream = new System.IO.MemoryStream();
                            img.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);

                            string fileUrl = context.Server.MapPath("..\\..\\FileTemp\\SaleData\\qrcode") + "\\" + items[1] + ".png";


                            if (System.IO.File.Exists(fileUrl))
                            {
                                //存在文件
                                FileInfo file = new FileInfo(fileUrl);
                                file.Delete();
                            }
                            //不存在文件 
                            FileStream fs = new FileStream(fileUrl, FileMode.CreateNew, FileAccess.ReadWrite);
                            BinaryWriter bw = new BinaryWriter(fs, UTF8Encoding.UTF8);
                            byte[] by = MStream.ToArray();
                            for (int i = 0; i < MStream.ToArray().Length; i++)
                            {
                                bw.Write(by[i]);
                            }
                            fs.Close();
                            MStream.Close();
                            MStream.Dispose();
                            if (url != null && !string.IsNullOrEmpty(url))
                            {
                                urlList.Add(url);
                            }
                        }
                    }
                    List<string> listFJ = new List<string>();//保存附件路径
                    for (int i = 0; i < urlList.Count; i++)
                    {
                        string fileUrl = context.Server.MapPath("..\\..\\FileTemp\\SaleData\\qrcode") + "\\" + urlList[i];
                        listFJ.Add(fileUrl);
                    }
                    var name = "二维码";
                    var zipUrl = context.Server.MapPath("..\\..\\FileTemp\\SaleData\\qrcode") + "\\" + name + ".zip";
                    var zipName = name + ".zip";
                    var certPath = System.Web.HttpContext.Current.Server.MapPath("..\\..\\FileTemp\\SaleData\\qrcode") + "\\" + name + ".zip";
                    ZipFileMain(listFJ.ToArray(), urlList.ToArray(), zipUrl, 9);//压缩文件
                    return certPath;
                    //DownloadFile(context,zipName, zipUrl);//下载文件
                    //MakeCodeZip(urlList);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="fileName">要压缩的所有文件(完全路径)</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="name">压缩后文件路径</param>
        /// <param name="Level">压缩级别</param>
        protected void ZipFileMain(string[] filenames, string[] fileName, string name, int Level)
        {
            ZipOutputStream s = new ZipOutputStream(File.Create(name));
            Crc32 crc = new Crc32();
            //压缩级别
            s.SetLevel(Level); // 0 - store only to 9 - means best compression
            try
            {
                int m = 0;
                foreach (string file in filenames)
                {
                    //打开压缩文件
                    FileStream fs = File.OpenRead(file);//文件地址
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    //建立压缩实体
                    ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名
                    //时间
                    entry.DateTime = DateTime.Now;
                    //空间大小
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                    m++;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                s.Finish();
                s.Close();
            }
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="filePath"></param>
        protected void DownloadFile(System.Web.HttpContext context, string fileName, string filePath)
        {
            byte[] data = File.ReadAllBytes(filePath);
            MemoryStream stream = new MemoryStream(data);
            context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
            context.Response.BinaryWrite(stream.ToArray());
            stream.Close();
            stream.Dispose();
            //context.Response.End();
        }
        #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值