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
  • 57,407
  • 13
  • 129
  • 202

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
  • 24,753
  • 5
  • 47
  • 78