• ASP.NET生成静态页面


    /// <summary>
            /// 生成静态页面
            /// </summary>
            /// <param name="webUrl">url</param>
            /// <param name="savePath">保存本地的路径</param>
            /// <returns></returns>
            public static void AppendHTML(string webUrl, string savePath)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>(1);
                dict.Add(webUrl, savePath);
                AppendHTML(dict);
            }
            public static void AppendHTML(Dictionary<string, string> dict)
            {
                WebClient client = new WebClient();
                client.Credentials = CredentialCache.DefaultCredentials;  //获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
                HttpContext context = HttpContext.Current;
                foreach (KeyValuePair<string, string> pair in dict)
                {
                    string url = pair.Key.StartsWith("http://") ? pair.Key : "http://" + context.Request.ServerVariables["HTTP_HOST"] + pair.Key;
                    string path = pair.Value;
                    path = Path.GetDirectoryName(AppRuntime.MapPath(path));
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    byte[] buffer = client.DownloadData(url);
                    long length = buffer.LongLength;
                    if (length > int.MaxValue)
                    {
                        throw new IOException("FileTooLong2GB");
                    }
                    int count = (int)length;
                    using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
                    using (BinaryWriter writer = new BinaryWriter(stream, Encoding.Default))
                    {
                        writer.Write(buffer, 0, count);
                    }
                }
                client.Dispose();
            }
    

  • 相关阅读:
    JS和C# 里的闭包及闭包在事件中的使用
    ***项目开发记录
    七牛云存储之应用视频上传系统开心得
    二维码及二维码接合短URL的应用
    EF批量添加,删除,修改的扩展
    ngTemplateOutlet递归的问题
    每日新知2019-06-03
    Spring boot初始
    纯前端播放本地音乐
    macbook 安装任意来源
  • 原文地址:https://www.cnblogs.com/Googler/p/1913870.html
Copyright © 2020-2023  润新知