C# 将字节[]-数组转换为ANSI
我正在尝试使用C#和Microsoft Lightswitch将MSSQL数据库中的byte[]blob转换为Windows-1252 ANSI格式,并将结果返回到下载文件 这是我认为应该起作用的 我正在使用创建字符串C# 将字节[]-数组转换为ANSI,c#,sql-server,unicode,visual-studio-lightswitch,ansi,C#,Sql Server,Unicode,Visual Studio Lightswitch,Ansi,我正在尝试使用C#和Microsoft Lightswitch将MSSQL数据库中的byte[]blob转换为Windows-1252 ANSI格式,并将结果返回到下载文件 这是我认为应该起作用的 我正在使用创建字符串 System.Text.Encoding v_Unicode = System.Text.Encoding.Unicode; System.Text.Encoding v_ANSI_Windows_1252 = System.Text.Encoding.GetEncoding(1
System.Text.Encoding v_Unicode = System.Text.Encoding.Unicode;
System.Text.Encoding v_ANSI_Windows_1252 = System.Text.Encoding.GetEncoding(1252);
string v_Content_1252 = v_ANSI_Windows_1252.GetString(System.Text.Encoding.Convert(v_Unicode, v_ANSI_Windows_1252, v_Unicode.GetBytes(v_Content)));
byte[] ansiArray = v_ANSI_Windows_1252.GetBytes(v_Content_1252);
并将其写入数据库。当我试图用
int v_fileId = Int32.Parse(context.Request.QueryString["p_FileId"]);
DataTableName v_fexpd = serverContext.DataWorkspace.ApplicationData.DataTableName_SingleOrDefault(v_fileId);
MemoryStream memStream = new MemoryStream(v_fexpd.Inhalt);
string v_Content= System.Text.Encoding.GetEncoding(1252).GetString(v_fexpd.Content);
context.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + v_fexpd.Filename);
context.Response.Write( v_Content );
context.Response.End();
。。。但它只返回Unicode。我做错了什么?这是为任何有类似问题的人准备的。答案是越过一条小溪。。。我所做的是:
// Create a temporary file, delete if it already exists
string MyFile = Path.GetTempPath() + v_fexpd.Dateiname;
if (File.Exists(MyFile)) {
File.Delete(MyFile);
}
using (TextWriter tw = new StreamWriter(MyFile.ToString(), true, System.Text.Encoding.GetEncoding(1252)))
tw.WriteLine(v_Inhalt);
context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + v_fexpd.Dateiname);
context.Response.AddHeader("Content-Type", "text/csv; charset=windows-1252");
// Write the file - which at this point is correctly-encoded -
// directly into the output.
context.Response.WriteFile(MyFile);
context.Response.End();
// Leave no traces
File.Delete(MyFile);
这适用于任何有类似问题的人。答案是越过一条小溪。。。我所做的是:
// Create a temporary file, delete if it already exists
string MyFile = Path.GetTempPath() + v_fexpd.Dateiname;
if (File.Exists(MyFile)) {
File.Delete(MyFile);
}
using (TextWriter tw = new StreamWriter(MyFile.ToString(), true, System.Text.Encoding.GetEncoding(1252)))
tw.WriteLine(v_Inhalt);
context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + v_fexpd.Dateiname);
context.Response.AddHeader("Content-Type", "text/csv; charset=windows-1252");
// Write the file - which at this point is correctly-encoded -
// directly into the output.
context.Response.WriteFile(MyFile);
context.Response.End();
// Leave no traces
File.Delete(MyFile);
字符串的内容始终为UTF16System.Text.Encoding.GetEncoding(1252).GetString(v_fexpd.Content)
将从v_fexpd.Content
读取数据,将其视为ANSI并将其转换为UTF16。因此,对于我来说,真的没有办法将这个字节数组作为ANSI进行输出?那么v_fexpd.Content
不是您想要的字节数组吗?是的。但我需要将其作为字符串提供给context.Response.Write。。。或者我现在完全是傻瓜吗?string
不可能是UTF16以外的任何东西,所以我不知道你怎么做。string
的内容将始终是UTF16System.Text.Encoding.GetEncoding(1252).GetString(v_fexpd.Content)
将从v_fexpd.Content
读取数据,将其视为ANSI并将其转换为UTF16。因此,对于我来说,真的没有办法将这个字节数组作为ANSI进行输出?那么v_fexpd.Content
不是您想要的字节数组吗?是的。但我需要将其作为字符串提供给context.Response.Write。。。或者我现在完全是个傻瓜吗?string
除了UTF16之外,没有其他方法,所以我不知道你怎么做。