asp.net中如何创建JS文件?
我是这样写的:
public void cj(Object sender,EventArgs e)
{
int shuliang=Int32.Parse(ts.Text);
string sqlstring= "select top "+shuliang+ "bt,url_shtml from news where lx= ' "+ddl.SelectedItem.Text+ " ' order by ID DESC ";
DataSet ds=new DataSet();
SqlDataAdapter adap=new SqlDataAdapter(sqlstring,conn);
adap.Fill(ds, "news ");
StreamWriter sw=File.CreateText(Server.MapPath( "../JS/ "+ddl.SelectedItem.Value+ ".js "));
//FileStream fs=new FileStream(Server.MapPath( "../JS/ "+ddl.SelectedItem.Value+ ".js ",FileMode.Create,FileAccess.Write));
//StreamWriter sw=new StreamWriter(fs,Encoding.Default);
for(int i=0;i <shuliang;i++)
{
sw.WriteLine( "document.write( ' <a href=shtml/news/ "+ds.Tables[ "news "].Rows[i][ "url_shtml "].ToString()+ " target=_blank> "+ds.Tables[ "news "].Rows[i][ "bt "].ToString()+ " </a> ') ");
}
sw.Flush();
sw.Close();
}
可是创建的JS文件乱码!怎么回事啊???在线狂等!!!
------解决方案--------------------我觉得你要是写文本用TextWrite比较好,因为stream write一般是用来写入byte的,或者你将字符串转为byte数组好了,用System.Text.Encording,这样