写了一个方法,主要实现WEB文件生成的功能,真正使用中一次性可能会生成1000以上个HTML文件。
方法如下:
 /// <summary>
    /// 生成文件
    /// </summary>
    /// <param name="FileFullName">文件全路径</param>
    /// <param name="FileContent">文件内容</param>
    public void WriteLine(string FileFullName, string FileContent)
    {
        //byte[] buffer = gb.GetBytes(FileContent);
        string dir = FileFullName.Substring(0, FileFullName.LastIndexOf("\\"));
        if (!Directory.Exists(dir))
        {
            //判断目录是否存在如果不存在则创建
            Directory.CreateDirectory(dir);
        }
        StreamWriter myStrWrite = new StreamWriter(FileFullName,false);
        myStrWrite.WriteLine(FileContent);
        myStrWrite.Close();        /*
         * 考虑到大批量文件的处理生成方式更换
        FileStream fs = new FileStream(FileFullName, FileMode.Create);
        fs.Write(buffer, 0, buffer.Length);
        fs.Flush();
        fs.Close();
         */
    }问题:无论使用StreamWriter 还是使用FileStream 进行生成。速度都很慢,一般生成500个文件时就好像卡死了。生成少量文件没有问题。寻求高效的解决方案。谢谢!!

解决方案 »

  1.   

    经过反复测试,我发现问题不是StreamWriter 和FileStream不高效。我是使用JQuery ajax异步处理的。并设置了超时时间60分钟(因为服务器端处理时间较长)。在生成500个页面 以内AJAX能够正确接收到服务器传回正确的信息。超过500个页面就无法接收到服务器传回的信息了,但是页面也确实正常的生成了。这种情况如何解决呢!!
    下面是AJAX内容
    //批量生成新闻function createNews(classId)
    {
        $.ajax({
       type: "POST",
       url: "News.aspx",
       dataType: "text",
       timeout: 600000,
       data: "classId="+classId+"&Event=create&random="+Math.random(),
       success: function(html){
          if(html!="false")
        {
           //处理成功
           $("#btnShow").html("");    
           $("#emStatus").html("<br/><img src='../images/checked.gif' align='absmiddle' />&nbsp;"+html+"<br/>");
        }
        else
        {
           $("#emStatus").html("<br/><img src='../images/unchecked.gif' align='absmiddl'/>&nbsp;页面生成失败请重新生成!<br/>");
        }
       }
    }); }