binaray阅读器问题,二进制阅读器不读取字节。 [英] binaray reader problem ,binary reader not reading bytes.

查看:127
本文介绍了binaray阅读器问题,二进制阅读器不读取字节。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码运行良好

会话[  fileUpl] = fileUpl; lblFileUpload.Text = fileUpl.FileName; 
会话[ 图像] = fileUpl.PostedFile;
Stream fs = fileUpl.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
byte [] bytes = br.ReadBytes(( Int32 )fs.Length);
string base64String = Convert.ToBase64String(bytes, 0 ,bytes.Length);
Image1.ImageUrl = data:image / png; base64, + base64String;





但是下面这段代码不起作用,fs.length显示文件长度但是bites数组显示0字节。这意味着二进制阅读器不开心有什么。我不知道为什么请帮忙。



 fileUpl =(FileUpload)Session [  fileUpl]; lblFileUpload.Text = fileUpl.FileName; 
HttpPostedFile postedFile =(HttpPostedFile)Session [ Image];
Stream fs = postingFile.InputStream;
BinaryReader br = new BinaryReader(fs);
byte [] bytes = br.ReadBytes(( Int32 )fs.Length);
string base64String = Convert.ToBase64String(bytes, 0 ,bytes.Length);
Image1.ImageUrl = data:image / png; base64, + base64String;

解决方案

将PostedFile的byte []放入会话并在下次访问时

 Stream fs = fileUpl.PostedFile.InputStream; 
BinaryReader br = new BinaryReader(fs);
会话[ bytes] = br.ReadBytes(( Int32 )fs.Length);



然后

  byte  [] bytes =( byte  [])会话[  bytes] 
string base64String = Convert.ToBase64String(bytes, 0 ,bytes.Length);
Image1.ImageUrl = data:image / png; base64, + base64String;


This code works perfectly well

Session["fileUpl"] = fileUpl; lblFileUpload.Text = fileUpl.FileName;
            Session["Image"] = fileUpl.PostedFile;
            Stream fs = fileUpl.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
            Image1.ImageUrl = "data:image/png;base64," + base64String;



but this code below does not works ,fs.length shows file lenght but bites array shows 0 bytes .that means binary reader is not happy with something .i dont know why please help.

fileUpl = (FileUpload)Session["fileUpl"]; lblFileUpload.Text = fileUpl.FileName;
           HttpPostedFile postedFile = (HttpPostedFile)Session["Image"];
           Stream fs = postedFile.InputStream;
           BinaryReader br = new BinaryReader(fs);
           byte[] bytes = br.ReadBytes((Int32)fs.Length);
           string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
           Image1.ImageUrl = "data:image/png;base64," + base64String;

解决方案

put byte[] of PostedFile to the session and access it next time

Stream fs = fileUpl.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Session["bytes"] = br.ReadBytes((Int32)fs.Length);


then

byte[] bytes = (byte[])Session["bytes"]
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;


这篇关于binaray阅读器问题,二进制阅读器不读取字节。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆