将 byte[]-Array 转换为 ANSI

Converting a byte[]-Array to ANSI

我正在尝试使用 C# 和 Microsoft Lightswitch 将 MSSQL 数据库中的 byte[] blob 转换为 Windows-1252 ANSI 格式,并将结果return 下载到文件中。

这就是我认为应该有效的...

我正在用

创建字符串
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();

...但它只是 returns Unicode。我做错了什么?

这适用于遇到类似问题的任何人。答案是通过 Stream...我所做的如下:

// 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);