zoukankan      html  css  js  c++  java
  • 上传图片更新

    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

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

  • 相关阅读:
    这是一个关于Latex的测试
    在Mac下配置php开发环境:Apache+php+MySql
    CSS 颜色代码大全
    APP中关于Android和IOS与网页交互
    linux crontab
    dedecms中的内容页中的变量
    lamp中的Oracle数据库链接
    phpcms使用session的方法
    linux系统下nginx安装目录和nginx.conf配置文件目录
    window7+wamp环境配置Oracle数据库连接
  • 原文地址:https://www.cnblogs.com/feiyucha/p/12545746.html
Copyright © 2011-2022 走看看