二进制流'0'不包含有效的BinaryHeader。无效的流或对象版本更改

时间:2014-02-12 10:50:05

标签: c#

我知道这个问题已经存在但是它们对我没有帮助,我希望将一个动作文件发布到ConvertToFLV动作然后返回一个转换为byte []的对象,现在当我想将它转换为对象时我得到了在ByteArrayToObject方法中跟随错误,当到达此行时视频obj =(视频)binForm.Deserialize(memStream)
二进制流'0'不包含有效的BinaryHeader。可能的原因是序列化和反序列化之间的无效流或对象版本更改 另外请告诉我在ConvertToFLV动作中contentType是否正确?

public ActionResult Index()
        {
            WebRequest request = WebRequest.Create("http://localhost:25220/home/ConvertToFLV");
            request.Method = "POST";
            string path = Request.MapPath("~") + "2.wmv";
            byte[] byteArray = System.IO.File.ReadAllBytes(path);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();
            byte[] convertedBytes = new byte[(int)response.ContentLength];
            dataStream.Read(convertedBytes, 0, (int)response.ContentLength);
            var obj = ByteArrayToObject(convertedBytes);
            dataStream.Close();
            response.Close();
            return View();
        }
public FileResult ConvertToFLV(HttpPostedFileBase file)
        {
            Stream stream = HttpContext.Request.InputStream;
            byte[] result = new byte[HttpContext.Request.ContentLength];
            stream.Read(result, 0, HttpContext.Request.ContentLength);
            Video video = new Video { FLV = result };
            return File(ObjectToByteArray(video), "application/x-www-form-urlencoded");
        }
 private Object ByteArrayToObject(byte[] arrBytes)
        {
            MemoryStream memStream = new MemoryStream();
            memStream.Position = 0;
            BinaryFormatter binForm = new BinaryFormatter();
            memStream.Write(arrBytes, 0, arrBytes.Length);
            memStream.Seek(0, SeekOrigin.Begin);
            Video obj = (Video)binForm.Deserialize(memStream);
            return obj;
        }

    private byte[] ObjectToByteArray(Video obj)
    {
        if (obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }

0 个答案:

没有答案