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