Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 ASP.NET4.0
hi..
how to convert data string to byte[]. i doing downloading file in c#. but i got an error cannot implicit convert type 'string' to 'byte[]' how can i solve this problem...
 
this is my sample coding
 string filename = filename;
            
            Byte[] bytes = attachment;
            try
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite(bytes);
                Response.Flush();
            }
            catch (Exception ex)
            {
                Response.ContentType = "text/plain";
                Response.Write(ex.Message);
            }
            finally
            {
                Response.End();
            }
i got an error at Byte[] bytes = attachment
 

Thanks in advance
Posted 9-Feb-13 1:59am
NaniCh684
Edited 9-Feb-13 2:10am
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

string to byte[]:
   string s = ...
   byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
Depending on what your file was written with, you may want to use one of the other Encoding options instead of ASCII. Unicode, UTF7 or UTF8 perhaps.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Use the Encoding.GetBytes Method[^]
byte[] array = Encoding.yourEncoding.GetBytes(stringData);
Change yourEncoding into the encoding you want to use.
For example:
byte[] array = Encoding.UTF8.GetBytes(stringData);
 
Another way:
 
byte[] array = Encoding.GetEncoding("encodingName").GetBytes(stringData);
For example:
byte[] array = Encoding.GetEncoding("utf-32").GetBytes(stringData);
Hope this helps.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

write
byte[] bytes = attachment;
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid