将字节[]-Array转换为ANSI
本文关键字:转换 ANSI -Array 字节 | 更新日期: 2023-09-27 18:10:41
我正在尝试使用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(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);