如何使用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 然后正常上传分析报文 模拟一个相同的报文就好了

时间: 2024-10-11 10:37:40

如何使用c#上传base64编码的数据的相关文章

上传base64编码文件

1.js进行图片预览 使用input标签来选择图片,使用FileReader读取图片并转成base64编码,然后发送给服务器. <html> <body> <img id="image"src=""/> <br/> <input type="file"onchange="selectImage(this);"/> <br/> <input type=

接口上传base64编码图片

1 package com.*.util; 2 3 import java.io.FileInputStream; 4 5 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 import java.util.Date; 11 12 import Decoder.BASE64Decoder;

上传base64图片到七牛云前端遇到的坑

介意前端普通引入七牛云SDk上传图片到七牛云需要多个js,所以才有了base64的上传方式,简化操作,(懒.) 七牛云官方文档如下 https://developer.qiniu.com/kodo/kb/1326/how-to-upload-photos-to-seven-niuyun-base64-code 我们前端理所当然就复制黏贴了..然后问题就来了 看地址 var url = "http://upload.qiniu.com/putb64/20264"; //非华东空间需要根据

C#解码base64编码二进制数据的方法

在Convert类的静态方法Convert.FromBase64String,可以讲base64编码的字符串解码为等效的byte []数组. using System; static class MyModClass { public static byte[] Base64DecodeString(this string inputStr) { byte[] decodedByteArray = Convert.FromBase64String(inputStr); return (decod

C# WPF ASP.net 上传多文件和数据

C# WinForm 上传多文件和数据 public static class HttpHelper { private static readonly Encoding DEFAULTENCODE = Encoding.UTF8; /// <summary> /// HttpUploadFile /// </summary> /// <param name="url"></param> /// <param name="

vue实现拍照上传 + base64 上传功能

<style> .head_pic{ width: 200px; height: 200px; background: #fff; /* border: 1px solid #e1e1e1; */ border-radius: 8px; margin: 10px auto; overflow: hidden; } .head_pic img{ width: 100%; height: 100%; } .upload_warp_text span{ display: block; font-si

jquery上传base64位图片

<img id="articleImg" width="180" height="100"> <input type="file" value="上传" id="articleImg 1 $('#articleImgBtn').change(function(){ 2 run(this, function (data) { 3 uploadImage(data); 4 }); 5

从原理上理解Base64编码

开发者对Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了.实际 上Base64已经简单到不能再简单了,如果对它的理解还是模棱两可实在不应该.大概介绍一下Base64的相关内容,花几分钟时间就可以彻底理解它.文 章下边贴了一个Base64的编解码器,方便阅读文章的同时来实验. 一. Base64编码由来 为什么会有Base64编码呢?因为有些网络传送渠道并不支持所有的字节,例如传统的邮件只支持可见字符的传送,像ASCII码的控制字符就 不能通过邮件传送.这样用途就受到了很大的限制,比如

上传base64图片并压缩

elementUI+react 布局 <Dialog title="充值" visible={ dialogVisible } onCancel={ () => this.setState({ dialogVisible: false }) } > <Dialog.Body> <Form labelWidth="120" ref={ e => {this.form=e} } model={ form } rules={ ru