原文:c# 上传图片到一个外链相册服务器
这里一个免费上传图片的网站:https://imgbb.com
代码:
代码语言:javascript复制private void post1(string filePath)
{
try
{
string fName = new FileInfo(filePath).Name;
string url = @"https://imgbb.com/json";
string source = fName;
string auth_token = "ff5ee7541280debc0674220f604c8441829a7f68";//auth_token 用抓包或者浏览器F12得到。
string timestamp = "" ConvertDateTimeToInt(DateTime.Now);
string fileName = fName;
string action = "upload";
string type = "file";
byte[] fileContentByte = new byte[1024]; // 文件内容二进制
#region 将文件转成二进制
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
fileContentByte = new byte[fs.Length]; // 二进制文件
fs.Read(fileContentByte, 0, Convert.ToInt32(fs.Length));
fs.Close();
#endregion
#region 定义请求体中的内容 并转成二进制
string boundary = "WebKitFormBoundaryQpiD7Zd4EUOicN44";
string Enter = "rn";
string modelIdStr = "--" boundary Enter
"Content-Disposition: form-data; name="source"" Enter Enter
source Enter;
string fileContentStr = "--" boundary Enter
"Content-Type:image/jpeg" Enter
"Content-Disposition: form-data; name="source"; filename="" fileName """ Enter Enter;
string updateTimeStr = Enter "--" boundary Enter
"Content-Disposition: form-data; name="timestamp"" Enter Enter
timestamp;
string enAction = Enter "--" boundary Enter
"Content-Disposition: form-data; name="action"" Enter Enter
action;
string enType = Enter "--" boundary Enter
"Content-Disposition: form-data; name="type"" Enter Enter
type;
string enauth_token = Enter "--" boundary Enter
"Content-Disposition: form-data; name="encrypt"" Enter Enter
auth_token Enter "--" boundary "--";
var modelIdStrByte = Encoding.UTF8.GetBytes(modelIdStr);//modelId所有字符串二进制
var fileContentStrByte = Encoding.UTF8.GetBytes(fileContentStr);//fileContent一些名称等信息的二进制(不包含文件本身)
var updateTimeStrByte = Encoding.UTF8.GetBytes(updateTimeStr);//updateTime所有字符串二进制
var encryptAction = Encoding.UTF8.GetBytes(enAction);//enAction所有字符串二进制
var encryptType = Encoding.UTF8.GetBytes(enType);//enType所有字符串二进制
var encryptStrByte = Encoding.UTF8.GetBytes(enauth_token);//encrypt所有字符串二进制
#endregion
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "multipart/form-data;boundary=" boundary;
Stream myRequestStream = request.GetRequestStream();//定义请求流
#region 将各个二进制 安顺序写入请求流 modelIdStr -> (fileContentStr fileContent) -> uodateTimeStr -> encryptStr
myRequestStream.Write(modelIdStrByte, 0, modelIdStrByte.Length);
myRequestStream.Write(fileContentStrByte, 0, fileContentStrByte.Length);
myRequestStream.Write(fileContentByte, 0, fileContentByte.Length);
myRequestStream.Write(updateTimeStrByte, 0, updateTimeStrByte.Length);
myRequestStream.Write(encryptAction, 0, encryptAction.Length);
myRequestStream.Write(encryptType, 0, encryptType.Length);
myRequestStream.Write(encryptStrByte, 0, encryptStrByte.Length);
#endregion
HttpWebResponse response = (HttpWebResponse)request.GetResponse();//发送
Stream myResponseStream = response.GetResponseStream();//获取返回值
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
MessageBox.Show(retString);
}
catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 将c# DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time">时间</param>
/// <returns>long</returns>
public static long ConvertDateTimeToInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}