[英]Converting a byte[]-Array to ANSI
I am trying to convert a byte[] blob in an MSSQL database to Windows-1252 ANSI format using C# and Microsoft Lightswitch, and return the results to a file download. 我正在尝试使用C#和Microsoft Lightswitch将MSSQL数据库中的byte [] blob转换为Windows-1252 ANSI格式,并将结果返回到文件下载中。
This is what I thought should work... 我认为这应该起作用...
I'm creating the string with 我用创建字符串
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);
and write it in the database. 并将其写入数据库。 When I try to retrieve with
当我尝试使用
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();
... but it just returns Unicode. ...但是它只返回Unicode。 What am I doing wrong?
我究竟做错了什么?
This is for anyone having a similar problem. 这适用于有类似问题的任何人。 The answer is to go the way over a Stream... What I did is the following:
答案是通过一条流……我所做的是:
// 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.