FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + filename);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();功能 IE中显示txt文档的内容,文档格式已经设置了,为什么 读出来的还是乱码?
哪个地方 有问题?
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + filename);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();功能 IE中显示txt文档的内容,文档格式已经设置了,为什么 读出来的还是乱码?
哪个地方 有问题?
System.Text.Encoding.GetEncoding("gb2312")
File.ReadAllLines(@"C:\abc.html", System.Text.Encoding.Default);
没有设置第二个参数System.Text.Encoding.Default
http://technet.microsoft.com/zh-cn/subscriptions/bsy4fhsa.aspx 参考
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
byte[] buffer = new byte[stream.Length];
try
{
stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
string contentType = "application/octet-stream";
string strName = Path.GetFileName(path);
string ext = Path.GetExtension(strName);
Microsoft.Win32.RegistryKey registryKey =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (registryKey != null && registryKey.GetValue("Content Type") != null)
contentType = registryKey.GetValue("Content Type").ToString();
HttpResponse response = this.Response;
response.ContentType = contentType;
response.AppendHeader("Content-Disposition", String.Concat("attachment;filename=", HttpUtility.UrlEncode(strName)));
response.BinaryWrite(buffer);
response.End();
}
catch
{ }
finally
{
stream.Dispose();
}
Response.ContentEncoding = System.Text.Encoding.Default;
Response.WriteFile(Server.MapPath("~/哪几类人最容易被提拔重用?.txt"));一般记事本中创建的txt默认是gb2312的。直接这样写就行了。
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
子夜大神 被推荐mvp了编码的话 看看乱码是什么样的 有的是根本没识别的 有的是 转来转去 转乱的 具体是那种 要看乱码是什么样的 做java的时候 被乱码恶心坏了
我刚 用IE直接打开txt文档没问题,用上面的方法打开 就不行