当前位置: 代码迷 >> C# >> 怎么使用c#上传图片,图片是base64编码的字符串
  详细解决方案

怎么使用c#上传图片,图片是base64编码的字符串

热度:65   发布时间:2016-05-05 04:04:30.0
如何使用c#上传图片,图片是base64编码的字符串
using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Threading.Tasks;namespace Test{    class Program    {        static void Main(string[] args)        {                 string inputStr = "iVBORw0KGgoAAAANSUhEUgAAAPgAAADMCAYAAABeOm6wAAAFj0lEQVR4Xu3bQY0DQRTE0DR/HjkMnT0PhaGQaAPD9cKg7G+N1FLO5+/9efkhgECSwBF40qtRCPwICNwhIBAmIPCwXNMQELgbQCBMQOBhuaYhIHA3gECYgMDDck1DQOBuAIEwAYGH5ZqGgMDdAAJhAgIPyzUNAYG7AQTCBAQelmsaAud5Hv8mcwcIRAkIPCrWLAT+CQjcHSAQJiDwsFzTEBC4G0AgTEDgYbmmIXCu6/KK7g4QiBIQeFSsWQj8XtF9wR0CAl0CAu+6tQwBX3A3gECZwLnv2yNb2bBt0wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdNQODT+o2vExB43bB90wQEPq3f+DoBgdcN2zdN4Auv/dj2RT7ULwAAAABJRU5ErkJggg==";            string time = "Ef1ae0cH2GI3Ef1cH2GI3KM7Ij5gL6";//DateTime.Now.Ticks.ToString("x");            string boundary = "----------" + time;            string boundary2 = "------------" + time;            string uploadUrl = "**";            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);            webrequest.Headers["X-Requested-With"] = "ShockwaveFlash/17.0.0.134";            webrequest.Headers["Accept-Encoding"] = "gzip, deflate";            webrequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36";            webrequest.Accept = "*/*";            webrequest.ContentType = "multipart/form-data; boundary=" + boundary;            webrequest.Method = "POST";            StringBuilder sb = new StringBuilder();            sb.Append("--");            sb.Append(boundary);            sb.Append("\r\n");            sb.Append("Content-Disposition: form-data; name=\"Filename\"");            sb.Append("\r\n");            sb.Append("\r\n");            sb.Append("1.png");            sb.Append("\r\n");            sb.Append("--");            sb.Append(boundary);            sb.Append("\r\n");            sb.Append("Content-Disposition: form-data; name=\"Filedata\"; filename=\"1.png\"");            sb.Append("\r\n");            sb.Append("Content-Type: application/octet-stream");            sb.Append("\r\n");            sb.Append("\r\n");            StringBuilder bs = new StringBuilder();            bs.Append("\r\n");            bs.Append("--");            bs.Append(boundary);            bs.Append("\r\n");            bs.Append("Content-Disposition: form-data; name=\"Upload\"");            bs.Append("\r\n");            bs.Append("\r\n");            bs.Append("Submit Query");            bs.Append("\r\n");            bs.Append("--");            bs.Append(boundary + "--");            string postHeader = sb.ToString();            string postEnder = bs.ToString();            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);            byte[] boundaryBytes = Encoding.ASCII.GetBytes(postEnder);            byte[] buffer = GetByteImage(inputStr);            long length = postHeaderBytes.Length + boundaryBytes.Length + buffer.Length;            webrequest.ContentLength = length;            Stream requestStream = webrequest.GetRequestStream();            requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);            requestStream.Write(buffer, 0, buffer.Length);            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);            WebResponse response = webrequest.GetResponse();            StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//实例华一个流的读写器            Console.WriteLine(sr.ReadToEnd());            response.Close();            requestStream.Close();            Console.Read();        }        private static byte[] GetByteImage(string inputStr)        {            byte[] arr = Convert.FromBase64String(inputStr);            using (MemoryStream ms = new MemoryStream(arr))            {                using (Bitmap bmp = new Bitmap(ms))                {                    using (MemoryStream stream = new MemoryStream())                    {                        bmp.Save(stream, ImageFormat.Png);                        stream.Position = 0;                        byte[] buffer = new byte[stream.Length];                        stream.Read(buffer, 0, buffer.Length);                        stream.Flush();                        return buffer;                    }                }            }        }    }}

公司提的新需求 上传剪贴板的图片

出于公司隐私保护这里我不贴图片服务器接口的地址,主要这个地址没设权限

剪贴板取出来的数据是一个base64的字符串。经过一上午努力写出上传代码如上。

这里易出错的地方有2点

1.base64转出来的byte数组不能直接拿来写入stream必须将这个byte数组转换为bitmap再转成MemoryStream
写入stream才可以。

2.webrequest.ContentType的-比下面报文中的-少2个。

面对这种需求 装一个fiddler2 然后正常上传分析报文 模拟一个相同的报文就好了


  相关解决方案