winForm 图片base64编码转换上传文件到web服务器

   上一篇文件简单剖析了文件上传php的处理制度,它是建立在客户端浏览器的 file控件,其实客户端浏览器的file控件对文件是做了流读取的,把文件读取到浏览器然后通过http的 请求头的request payLoad 来向服务端请求。那么我们能不能不用file控件,我直接把流读成其他字符格式来向服务器端请求POST发送呢。奔着这个思路,用winFrom->http 服务器形式来模拟实现(本来浏览器是可以实现但是考虑到js来度本地文件 有点不大可能,另外可以actix来实现但是那样麻烦)

 一. winFrom 窗体不仅如下图:

第一个按钮点击后主要处理过程有:读入本地图片到流,然后把流转换的base64编码,显示在下面的那个textbox里头
C#代码如果下:
  
View Code
        void Button1Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK) {
                this.textBox1.Text=ImgToBase64String( this.openFileDialog1.FileName);
            }
        }
        
        //图片 转为    base64编码的文本
        private string ImgToBase64String(string Imagefilename)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);
                this.pictureBox1.Image = bmp;
      
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                string strbaser64 = Convert.ToBase64String(arr);
               return strbaser64;
            }
            catch (Exception ex)
            {
               return ("ImgToBase64String 转换失败\nException:" + ex.Message);
            }
        }

"显示图片"那个按钮是把textbox里面的base64字符在转换成二进制流,然后在给Bitmap对想方向显示代码如下:

View Code
         //base64编码的文本 转为    图片
        private void Base64StringToImage(string inputStr)
        {
            try
            {
                byte[] arr = Convert.FromBase64String(inputStr);
                MemoryStream ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);
                ms.Close();
                this.pictureBox2.Image = bmp;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Base64StringToImage 转换失败\nException:" + ex.Message);
            }
        }

点击上传按钮就是通过 HttpWebRequest向相应web服务器发送POST请求 (CollectHelper)为封装的http请求类:

View Code
void Tb_upClick(object sender, EventArgs e)
        {
            string png=this.textBox1.Text.ToString();
            string name=DateTime.Now.ToFileTimeUtc().ToString();
            CollectHelper htmlHelper = new CollectHelper();
            string url="http://localhost:81/pic.php";
            string parm="png="+htmlHelper.UrlEncode(png)+"&name="+name;
            string result = htmlHelper.CollectHtml(url, parm, CollectHelper.Method.POST, CollectHelper.HtmlEncoding.UTF8, CollectHelper.HtmlEncoding.UTF8);
            if(result!="timeout"){
                MessageBox.Show("上传成功");
            }else{
                MessageBox.Show("发送超时");
            }
        }

从代码里头容易看出图片的base64字符请求到了参数png下

二、服务器端

 思路:通过$_POST接受客户端的post请求字符串,然后base64转成二进制流,然后写入到图片文件进行保存:

View Code
function base64_to_png($inputfile, $outputfile ) {
    $imageData = $inputfile;
    $file = fopen( $outputfile, "w" );
    fwrite( $file, base64_decode( $imageData ) );
    fclose($file);
}
base64_to_png("".$_POST['png'], $_POST["name"].".png");

服务器端echosong 比较喜欢用 也习惯用php 其内置有 base64_decode,base64_encode 解码 编码的函数用起来非常方便快捷,而且内置的东西性能也给力

三、效果:

  

服务器端里面拿到文件

  

  

 

  

posted @ 2013-01-01 15:04  EchoSong  阅读(1266)  评论(0编辑  收藏  举报