0

I have JQ function that send me file (I try to send path, but I need to send file. Now i need to receive in C# and convert to byte array.

If I have something like:

$('#i_submit').click(function (event) {
  $.ajax({
    url: "Main/CP_Upload",
    data: { "name": name,"type":type,"file":file }
  });
});

(I check it is work, get file)

Can i receive it like

public void CP_Upload(string name,string type,File file)

(I get data, just I don't know is System.IO.File type that i need for definition of variable file...) Other question is can i type System.IO.File convert to byte array?

Thanx

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • If you *actually* have a file/stream, see http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream – user2864740 Sep 12 '14 at 05:53
  • 2
    ...isn't ajax uploading like this not supported in browsers? I would say you're receiving a file name.. not a stream of bytes. – Simon Whitehead Sep 12 '14 at 05:54

1 Answers1

0

File.ReadAllBytes(string path) will give you all the bytes from the file.

Alternatively you can give in FileInfo parameter, and read each byte: (example)

  var fi = new FileInfo(path);
  using (FileStream fs = fi.OpenRead()) 
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0) 
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78