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

时间:2014-09-12 05:52:17

标签: c# ajax file

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

如果我有类似的话:

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

(我检查是工作,获取文件)

我可以像

那样收到它
public void CP_Upload(string name,string type,File file)

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

感谢名单

1 个答案:

答案 0 :(得分:0)

File.ReadAllBytes(string path)将为您提供文件中的所有字节。

或者,您可以提供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));
            }
        }
相关问题