Return file download from byte[]

允我心安 提交于 2019-12-31 01:48:09

问题


This code

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

Turns out the string encoding is incorrect after it's converted into byte[]

So I need to put the string immediately into file, like this

FileStream xfile = new FileStream(Path.Combine(dldir, filename), FileMode.Create, System.IO.FileAccess.Write);
hssfwb.Write(xfile);

But I don't want to do this, I don't need the file after the download. I just need to return it to the browser as file download and don't want to have to deal with file deletion afterward which can become pretty hectic when there's a lot of request.

How to correct the character encoding from string to byte[] and correctly return it to the browser?

The GetBytes function looks like this

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;
}

回答1:


Something like this would work:

try
{
    Response.ContentType = "application/octet-stream"; 
    Response.AddHeader( "Content-Disposition", "attachment; filename=" + filename ); 
    Response.OutputStream.Write(xmlb, 0, xmlb.Length); 
    Response.Flush(); 
} 
catch(Exception ex) 
{
    // An error occurred.. 
}



回答2:


In this case:

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;
}

You will end up with UTF-16LE encoding, because that's what a char array is internally.

You should get rid of that function because it's both misleading and redundant duplication of functionality already existing out of the box because System.Text.Encoding.Unicode.GetBytes already does the same thing: An encoding for the UTF-16 format using the little endian byte order.

If creating a temporary file without specifying encoding works, then you probably want Windows-1252 because that was most likely being used implicitly when creating the file:

Encoding enc = Encoding.GetEncoding(1252);
byte[] xmlb = enc.GetBytes(xml);

If you wanted UTF-8 you would do:

byte[] xmlb = Encoding.UTF8.GetBytes(xml);



回答3:


I have had the answer for this, it turns out the problem was character encoding.

The solution link is below

Converting string to byte[] creates zero character



来源:https://stackoverflow.com/questions/14158804/return-file-download-from-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!