Возврат файла загрузки из байта []
Этот код
string xml = XmlHelper.ToXml(queryTemplate);
byte[] xmlb = StringHelper.GetBytes(xml);
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = String.Format("{0}_v{1}.xml", queryModel.Name, queryModel.Version),
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(xmlb, "application/xml");
Оказывается, кодировка строки после нее невернас превращается вbyte[]
Так что мне нужно поставитьstring
сразу в файл, вот так
FileStream xfile = new FileStream(Path.Combine(dldir, filename), FileMode.Create, System.IO.FileAccess.Write);
hssfwb.Write(xfile);
Но я нея не хочу этого делатьФайл не нужен после загрузки. Мне просто нужно вернуть его в браузер при загрузке файла и нене хочу иметь дело с удалением файла, которое может стать довольно беспокойным, когдамного запросов.
Как исправить кодировку символов изstring
вbyte[]
и правильно вернуть его в браузер?
GetBytes
функция выглядит так
public static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}