protected void btnExport_Click(object sender, EventArgs e) { string url = Request.Url.AbsoluteUri.Replace("MachiningForm", "snapshot"); Thread NewTh = new Thread(new ParameterizedThreadStart(ExportImg)); NewTh.SetApartmentState(ApartmentState.STA); NewTh.Start(url); while (NewTh.ThreadState == ThreadState.Running) { } MachiningDAL dal = new MachiningDAL(); var mach = dal.GetMachById(MachId); string fileName = string.Format("{0}.jpg", mach.Mach_No); string filePath = Server.MapPath(string.Format("~/ImgLib/{0}", fileName)); FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); }
void ExportImg(object url) { WebPageSnapshot wps = new WebPageSnapshot(); wps.Url = url.ToString(); try { MachiningDAL dal = new MachiningDAL(); var mach = dal.GetMachById(MachId); string fileName = string.Format("{0}.jpg", mach.Mach_No); wps.TakeSnapshot().Save(Server.MapPath("~/ImgLib/") + "\\" + fileName); } catch (Exception ex) { } wps.Dispose(); }