• 上传图片更新


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    namespace Web
    {
        public class Post
        {
            public static string HttpUploadFile(string id, string token, string filepath)
            {
    
                // 这个可以是改变的,也可以是下面这个固定的字符串 
                string boundary = "c808bb4d-264c-4484-bb95-11d72a277878";
                string url = "https://api.domain.cc/User.updateAvatar";
    
                // 创建request对象 
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
                webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
                webrequest.Method = "POST";
                webrequest.UserAgent = "okhttp-okgo/jeasonlzy";
    
                // 构造发送数据
                StringBuilder sb = new StringBuilder();
    
                // 文本域的数据
                sb.Append("--" + boundary);
                sb.Append("
    ");
                sb.Append("Content-Disposition: form-data; name="uid"");
                sb.Append("
    ");
                sb.Append("Content-Length: 6");
                sb.Append("
    
    ");
                sb.Append(id);
                sb.Append("
    ");
    
    
                // 文件域的数据
                sb.Append("--" + boundary);
                sb.Append("
    ");
                sb.Append("Content-Disposition: form-data; name="token"");
                sb.Append("
    ");
                sb.Append("Content-Length: 32");
                sb.Append("
    
    ");
                sb.Append(token);
                sb.Append("
    ");
                sb.Append("--" + boundary);
                sb.Append("
    ");
                sb.Append("Content-Disposition: form-data; name="file"; filename="20200205162320.png"");
                sb.Append("
    ");
                sb.Append("Content-Type: image/png");
                //少length ???
                sb.Append("
    
    ");
    
                string postHeader = sb.ToString();
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
    
                //构造尾部数据 
                byte[] boundaryBytes = Encoding.ASCII.GetBytes("
    --" + boundary + "--
    ");
    
                FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
                webrequest.ContentLength = length;
    
                Stream requestStream = webrequest.GetRequestStream();
    
                // 输入头部数据 
                requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
    
                // 输入文件流数据 
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    requestStream.Write(buffer, 0, bytesRead);
    
                // 输入尾部数据 
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                WebResponse responce = webrequest.GetResponse();
                Stream s = responce.GetResponseStream();
                StreamReader sr = new StreamReader(s);
    
                // 返回数据流(源码) 
                return sr.ReadToEnd();
            }
        }
    }
    $filePath='E:1.png'
    $url='https://api.domain.cc/updateAvatar'
    $fileBin=[System.IO.File]::ReadAllBytes($filePath)
    $en=[System.Text.Encoding]::GetEncoding("iso-8859-1")
    $fileEnc=$en.GetString($fileBin)
    $boundary=[System.Guid]::NewGuid().toString()
    $lf="`r`n"
    $bodyLines=(
        "--$boundary",
        "Content-Disposition: form-data; name=`"uid`"",
        "Content-Length: 6$lf",
        "310413",
        "--$boundary",
        "Content-Disposition: form-data; name=`"token`"",
        "Content-Length: 32$lf",
        "5aea7d9dbd15de0fd8b7e26fd8220e8b",
        "--$boundary",
        "Content-Disposition: form-data; name=`"file`"; filename=`"20200205162320.png`"",
        "Content-Type: image/png$lf",
        $fileEnc,
        "--$boundary--$lf"
    ) -join $lf
    
    $r=Invoke-RestMethod -Uri $url -Method Post -ContentType "multipart/form-data; boundary=$boundary" -UserAgent "okhttp-okgo/jeasonlzy" -Body $bodyLines

    补充:注意字节的编码方式 不然即使图片上传成功了 也无法正常显示

  • 相关阅读:
    qmake理解(还可以加入Lex Yacc文件)
    如何在Qt 4程序中优化布局结构(表格讲解,很清楚)
    QList内存释放(看它内部存储的是否是Object,另外还有qDeleteAll)
    Qt工具知多少(一目了然)
    分享个人如何DIY网站的经验
    推荐10款免费而优秀的图表插件
    异步上传文件
    JSON.stringify 方法
    Value Object(值对象)如何使用 EF 进行正确映射
    领域驱动设计(DDD)
  • 原文地址:https://www.cnblogs.com/feiyucha/p/12545746.html
Copyright © 2020-2023  润新知