繁体   English   中英

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

[英]How to read all bytes of a file from AJAX?

我有向我发送文件的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类型转换为字节数组吗?

谢谢

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM