当前位置: 首页 > news >正文

广州有专做网站建设游戏网站需要什么设备

广州有专做网站,建设游戏网站需要什么设备,唐山移动互联网开发,企业网盘软件目录 应用场景 实现代码 扩展功能(生成压缩包) 小结 应用场景 我们在一个求职简历打印的项目功能里,需要根据一定的查询条件,得到结果并批量导出指定格式的文件。导出的格式可能有多种,比如WORD格式、EXCEL格式、PDF格式等,…

目录

应用场景

实现代码

扩展功能(生成压缩包)

小结 


应用场景

我们在一个求职简历打印的项目功能里,需要根据一定的查询条件,得到结果并批量导出指定格式的文件。导出的格式可能有多种,比如WORD格式、EXCEL格式、PDF格式等,实现方式是通过设置对应的模板进行输出,实际情况是,简历的内容是灵活设置的,没有固定的格式,模板数量是不固定的。

通过动态页面技术,可以实现简历配置后的网页内容输出,但制作对应的各种模板会遇到开发效率和服务跟进的问题。为了保障原样输出,折中而简单的方案就是将动态输出的页面转化为图片格式。

实现代码

创建一个 UrlToImage 类,创建实例的时候传递指定的 URL, 并调用 SaveToImageFile(string outputFilename)方法,该方法传递要输出的文件名参数即可即可。

调用示例代码如下:

string url = "https://" + Request.Url.Host + "/printResume.aspx";
UrlToImage uti = new UrlToImage(url);
bool irv = uti.SaveToImageFile(Request.PhysicalApplicationPath + "\\test.jpg");
if(bool==false){Response.Write("save failed.");Response.End();
}

类及实现代码如下:

    public class UrlToImage{private  Bitmap m_Bitmap;private string m_Url;private string m_FileName = string.Empty;int initheight = 0;public UrlToImage(string url){// Without filem_Url = url;}public UrlToImage(string url, string fileName){// With filem_Url = url;m_FileName = fileName;}public Bitmap Generate(){// Threadvar m_thread = new Thread(_Generate);m_thread.SetApartmentState(ApartmentState.STA);m_thread.Start();m_thread.Join();return m_Bitmap;}public bool SaveToImageFile(string filename){Bitmap bt=Generate();if (bt == null){return false;}bt.Save(filename);return File.Exists(filename);}private void _Generate(){var browser = new WebBrowser { ScrollBarsEnabled = false };browser.ScriptErrorsSuppressed = true;initheight = 0;browser.Navigate(m_Url);browser.DocumentCompleted += WebBrowser_DocumentCompleted;while (browser.ReadyState != WebBrowserReadyState.Complete){Application.DoEvents();}browser.Dispose();}private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){// Capturevar browser = (WebBrowser)sender;browser.ClientSize = new Size(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);browser.ScrollBarsEnabled = false;m_Bitmap = new Bitmap(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);browser.BringToFront();browser.DrawToBitmap(m_Bitmap, browser.Bounds);// Save as file?if (m_FileName.Length > 0){// Savem_Bitmap.SaveJPG100(m_FileName);}if (initheight == browser.Document.Body.ScrollRectangle.Bottom){browser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);}initheight = browser.Document.Body.ScrollRectangle.Bottom;}}

生成压缩包

 对于批量生成的图片文件,我们可以生成压缩包为客户提供下载功能,压缩功能引用的是ICSharpCode.SharpZipLib.dll,创建 ZipCompress 类的实例,ZipDirectory(zippath, zipfile, password) 方法,需要提供的参数包括,压缩的目录、生成的压缩文件名,压缩包的打开密码。

示例代码如下:

    string zippath = Request.PhysicalApplicationPath + "\\des\\" ;if (!Directory.Exists(zippath)){Directory.CreateDirectory(zippath);}string zipfile = Request.PhysicalApplicationPath + "\\des\\test.zip";ZipCompress allgzip = new ZipCompress();System.IO.DirectoryInfo alldi = new System.IO.DirectoryInfo(zippath);string password = "123456";allgzip.ZipDirectory(zippath, zipfile, password);//以下是生成完压缩包后,清除目录及文件string[] allfs = Directory.GetFiles(zippath);for (int i = 0; i < allfs.Length; i++){File.Delete(allfs[i]);}Directory.Delete(zippath);  

类及实现代码如下:

 public class ZipCompress{public  byte[] Compress(byte[] inputBytes){using (MemoryStream outStream = new MemoryStream()){using (GZipStream zipStream = new GZipStream(outStream, CompressionMode.Compress, true)){zipStream.Write(inputBytes, 0, inputBytes.Length);zipStream.Close(); //很重要,必须关闭,否则无法正确解压return outStream.ToArray();}}}public  byte[] Decompress(byte[] inputBytes){using (MemoryStream inputStream = new MemoryStream(inputBytes)){using (MemoryStream outStream = new MemoryStream()){using (GZipStream zipStream = new GZipStream(inputStream, CompressionMode.Decompress)){zipStream.CopyTo(outStream);zipStream.Close();return outStream.ToArray();}}}}public  string Compress(string input){byte[] inputBytes = Encoding.Default.GetBytes(input);byte[] result = Compress(inputBytes);return Convert.ToBase64String(result);}public  string Decompress(string input){byte[] inputBytes = Convert.FromBase64String(input);byte[] depressBytes = Decompress(inputBytes);return Encoding.Default.GetString(depressBytes);}public  void Compress(DirectoryInfo dir){foreach (FileInfo fileToCompress in dir.GetFiles()){Compress(fileToCompress);}}public  void Decompress(DirectoryInfo dir){foreach (FileInfo fileToCompress in dir.GetFiles()){Decompress(fileToCompress);}}public  void Compress(FileInfo fileToCompress){using (FileStream originalFileStream = fileToCompress.OpenRead()){if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz"){using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz")){using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress)){originalFileStream.CopyTo(compressionStream);}}}}}public  void Decompress(FileInfo fileToDecompress,string desfilename=""){using (FileStream originalFileStream = fileToDecompress.OpenRead()){string currentFileName = fileToDecompress.FullName;string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);if (desfilename != ""){newFileName = desfilename;}using (FileStream decompressedFileStream = File.Create(newFileName)){using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress)){decompressionStream.CopyTo(decompressedFileStream);}}}}public  void ZipDirectory(string folderToZip, string zipedFileName,string password){ZipDirectory(folderToZip, zipedFileName,(password==""?string.Empty:password), true, string.Empty, string.Empty, true);}public  void ZipDirectory(string folderToZip, string zipedFileName, string password, bool isRecurse, string fileRegexFilter, string directoryRegexFilter, bool isCreateEmptyDirectories){FastZip fastZip = new FastZip();fastZip.CreateEmptyDirectories = isCreateEmptyDirectories;fastZip.Password = password;fastZip.CreateZip(zipedFileName, folderToZip, isRecurse, fileRegexFilter, directoryRegexFilter);}public void UnZipDirectory(string zipedFileName, string targetDirectory, string password,string fileFilter=null){FastZip fastZip = new FastZip();fastZip.Password = password;fastZip.ExtractZip(zipedFileName, targetDirectory,fileFilter);}public void UnZip(string zipFilePath, string unZipDir){if (zipFilePath == string.Empty){throw new Exception("压缩文件不能为空!");}if (!File.Exists(zipFilePath)){throw new FileNotFoundException("压缩文件不存在!");}//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  if (unZipDir == string.Empty)unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));if (!unZipDir.EndsWith("/"))unZipDir += "/";if (!Directory.Exists(unZipDir))Directory.CreateDirectory(unZipDir);using (var s = new ZipInputStream(File.OpenRead(zipFilePath))){ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(theEntry.Name);string fileName = Path.GetFileName(theEntry.Name);if (!string.IsNullOrEmpty(directoryName)){Directory.CreateDirectory(unZipDir + directoryName);}if (directoryName != null && !directoryName.EndsWith("/")){}if (fileName != String.Empty){using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name)){int size;byte[] data = new byte[2048];while (true){size = s.Read(data, 0, data.Length);if (size > 0){streamWriter.Write(data, 0, size);}else{break;}}}}}}}}

小结 

对于生成的图片文件,我们还可以结合其它的API应用,来判断图片是否有被PS的情况,来提升和扩展应用程序的功能。另外,对于被访问的动态页面,建议使用访问控制,只有正常登录或提供访问令牌的用户才可以生成结果图片,以保证数据的安全性。

以上代码仅供参考,欢迎大家指正,再次感谢您的阅读!

 

http://www.yayakq.cn/news/839091/

相关文章:

  • 建设网站需要买什么wordpress指定上传目录
  • 做网站详情的图片做网站柳州
  • 河北手机网站制作哪家好企业运营方案
  • 济南做网站公司电话wordpress4.7.5下载
  • 防城港网站设计南京网站建
  • 北京科技网站建设长沙房地产公司排名
  • 网站系统说明书广州市番禺区住房和建设局网站
  • 做网站时数据库要创建几个表暗网网站有那些
  • 北京市建设局网站wordpress修改固定连接404
  • 网站制作应用网络营销推广方法word
  • 丝绸之路网站平台建设入门做网站
  • 介绍商务网站开发流程写字就能赚钱做网站
  • 企业网站开发公司排行榜互联网建站公司有哪些
  • 网站开发的可行性分析天津企业做网站
  • 网站制作方案垂直领域获客莱芜东风街
  • 西部数码域名注册seo排名谁教的好
  • 请描述网站开发的一般流程广州番禺职业技术学院
  • 直播网站开发平台wordpress 主题和插件
  • 蓬莱网站建设价格ppt模板清新淡雅免费下载
  • 广州市网站建设报价互易中国如何做网站
  • 为什么做手机网站哈尔滨做网站哪家便宜
  • 泉州哪里建设网站wordpress网站转app
  • 网站开发成本预算山东省建设工程招标投标信息网
  • 网站副标题怎么写汽车网站模块
  • 可信网站认证必须做吗低价备案域名
  • wordpress综合类网站运营网站流程
  • 网站可以做动画轮播吗seo网站代码优化
  • 太原网站建设开发医院类网站建设与维护
  • 烟台高端网站建设公司destoon做众筹网站
  • 网站的链接结构怎么做wordpress漏洞 4.7