如何从AJAX读取文件的所有字节?

时间:2022-01-17 20:15:32

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.

我有发送文件的JQ函数(我尝试发送路径,但我需要发送文件。现在我需要在C#中接收并转换为字节数组。

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?

(我得到数据,只是我不知道是我需要的System.IO.File类型来定义变量文件...)其他问题是我可以输入System.IO.File转换为字节数组吗?

Thanx

1 个解决方案

#1


0  

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

File.ReadAllBytes(字符串路径)将为您提供文件中的所有字节。

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

或者你可以在FileInfo参数中给出,并读取每个字节:(示例)

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

#1


0  

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

File.ReadAllBytes(字符串路径)将为您提供文件中的所有字节。

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

或者你可以在FileInfo参数中给出,并读取每个字节:(示例)

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