由于项目中应用了一个资料管理系统,需要前期收集大家的建议
通过邮件或者问卷的形式效果不是很好,所以采用每次在用户访问网站的时候询问用户是否有建议
如果有,则保存用户的建议,如果没有,就进行用户自己后续的操作
但是这个系统是个已经发布好的网站,无法添加功能,而且也只是阶段使用
所以采用嵌入js代码,访问另一个网站,保存用户的数据
原网站嵌入js
<script type="text/javascript"> window.onload = function () { var a = confirm("您可以对该网站提一些建议吗?"); if (a==true) { window.open('http://192.168.xx.xxx'); } } </script>新建网站后台
public partial class Suggestion : System.Web.UI.Page { string path = System.Configuration.ConfigurationManager.AppSettings["SuggestionTxtPath"];//读取配置文件中路径地址 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.Form["user_name"] != null && Request.Form["user_suggestion"]!=null) { string name = Request.Form["user_name"].ToString(); string suggestion = Request.Form["user_suggestion"].ToString(); if (!File.Exists(path)) { FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//创建写入文件 StreamWriter sw = new StreamWriter(fs1); sw.WriteLine("时间 : "+DateTime.Now); sw.WriteLine(name + " : " + suggestion + "\n");//开始写入值 sw.WriteLine("\n"); sw.WriteLine("------------------------------------\n"); sw.WriteLine("\n"); sw.Close(); fs1.Close(); Response.Write("<script language=\"javascript\">alert(\"建议成功提交,感谢您 ~\\n(火狐浏览器需手动关闭此页面)\");if (navigator.userAgent.indexOf(\"MSIE\") > 0) {if (navigator.userAgent.indexOf(\"MSIE 6.0\") > 0) { window.opener = null;window.close();} else {window.open('', '_top'); window.top.close();}}else if (navigator.userAgent.indexOf(\"Firefox\") > 0) {window.location.href = 'about:blank ';} else {window.opener = null;window.open('', '_self', ''); window.close();}</script>"); } else { FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write); fs.Position = fs.Length; StreamWriter sr = new StreamWriter(fs); sr.WriteLine("时间 : " + DateTime.Now); sr.WriteLine(name + " : " + suggestion + "\n");//开始写入值 sr.WriteLine("\n"); sr.WriteLine("------------------------------------\n"); sr.WriteLine("\n"); sr.Close(); fs.Close(); Response.Write("<script language=\"javascript\">alert(\"建议成功提交,感谢您 ~\\n(火狐浏览器需手动关闭此页面)\");if (navigator.userAgent.indexOf(\"MSIE\") > 0) {if (navigator.userAgent.indexOf(\"MSIE 6.0\") > 0) { window.opener = null;window.close();} else {window.open('', '_top'); window.top.close();}}else if (navigator.userAgent.indexOf(\"Firefox\") > 0) {window.location.href = 'about:blank ';} else {window.opener = null;window.open('', '_self', ''); window.close();}</script>"); } } } } }新建网站配置文件
<configuration> <system.web> <compilation debug="true" targetFramework="4.0"/> <httpRuntime/> </system.web> <appSettings> <!--建议文本路径--> <add key="SuggestionTxtPath" value="D:\\网站建议.txt"/> </appSettings> </configuration>1.开始想要直接用js写txt文件,但是发现那是在客户端操作,服务器是无法获取的 解决:另外建立网站,采用表单提交,用类似日志记录功能记录建议
2.在本机可以运行,发布在服务器不可以运行 解决:本机framwork版本过高,降低版本即可;普通用户没有对服务器的文件的读写权限(修改文件/用户/应用池的权限) 参考博客: http://www.cnblogs.com/xiaoruilin/p/5147521.html http://blog.csdn.net/dongxiaohui2008/article/details/2838994 http://www.myexception.cn/dotnet exception/3580.html
3.浏览器无法自动关闭 解决:引用了一段兼容各大浏览器的js来帮助用户在填完建议后自动关闭页面,但是火狐浏览器由于自身的设置是不行的,最后只能显示白页却无法关闭 参考博客: http://blog.csdn.net/buster2014/article/details/46310335
因为用户都是内部人员,功能也比较小,就是用于临时收集一下建议 但是做这个小例子也是收获了一些关于读写、发布的知识点
