首页 文章

可以将Byte []数组写入C#中的文件吗?

提问于
浏览
296

我正在尝试写出一个表示文件完整文件的 Byte[] 数组 .

来自客户端的原始文件通过TCP发送,然后由服务器接收 . 接收的流被读取到字节数组,然后被发送以由该类处理 .

这主要是为了确保接收 TCPClient 为下一个流做好准备并将接收端与处理端分开 .

FileStream 类不将字节数组作为参数或另一个Stream对象(它允许您向其写入字节) .

我的目标是通过与原始线程(具有TCPClient的线程)不同的线程完成处理 .

我不知道如何实现这个,我该怎么办?

8 回答

  • 8

    是的,为什么不呢?

    fs.Write(myByteArray, 0, myByteArray.Length);
    
  • -1

    根据问题的第一句话:“我正在尝试将Byte []数组 representing a complete file 写入文件 . ”

    阻力最小的路径是:

    File.WriteAllBytes(string path, byte[] bytes)
    

    记录在这里:

    System.IO.File.WriteAllBytes - MSDN

  • 37

    有一个静态方法 System.IO.File.WriteAllBytes

  • 5

    您可以使用 BinaryWriter 对象 .

    protected bool SaveData(string FileName, byte[] Data)
    {
        BinaryWriter Writer = null;
        string Name = @"C:\temp\yourfile.name";
    
        try
        {
            // Create a new stream to write to the file
            Writer = new BinaryWriter(File.OpenWrite(Name));
    
            // Writer raw data                
            Writer.Write(Data);
            Writer.Flush();
            Writer.Close();
        }
        catch 
        {
            //...
            return false;
        }
    
        return true;
    }
    

    Edit: 哎呀,忘了 finally 部分......让我们说这是留给读者的练习;-)

  • 19

    您可以使用FileStream.Write(byte[] array, int offset, int count)方法将其写出来 .

    如果您的数组名称是“myArray”,那么代码就是 .

    myStream.Write(myArray, 0, myArray.count);
    
  • 635

    试试BinaryReader:

    /// <summary>
    /// Convert the Binary AnyFile to Byte[] format
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static byte[] ConvertANYFileToBytes(HttpPostedFileBase image)
    {
        byte[] imageBytes = null;
        BinaryReader reader = new BinaryReader(image.InputStream);
        imageBytes = reader.ReadBytes((int)image.ContentLength);
        return imageBytes;
    }
    
  • 11
    public ActionResult Document(int id)
        {
            var obj = new CEATLMSEntities().LeaveDocuments.Where(c => c.Id == id).FirstOrDefault();
            string[] stringParts = obj.FName.Split(new char[] { '.' });
            string strType = stringParts[1];
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("content-disposition", "attachment; filename=" + obj.FName);
            var asciiCode = System.Text.Encoding.ASCII.GetString(obj.Document);
            var datas = Convert.FromBase64String(asciiCode.Substring(asciiCode.IndexOf(',') + 1));
            //Set the content type as file extension type
            Response.ContentType = strType;
            //Write the file content
            this.Response.BinaryWrite(datas);
            this.Response.End();
            return new FileStreamResult(Response.OutputStream, obj.FType);
        }
    
  • 0

    你可以使用 System.IO.BinaryWriter 来做到这一点,它接受一个流:

    var bw = new BinaryWriter(File.Open("path",FileMode.OpenOrCreate);
    bw.Write(byteArray);
    

相关问题