有没有c#图片上传的例子
/// </summary>
/// <param name="url"> url = "https://oapi.dingtalk.com/media/upload?access_token=" + token + "&type=image";</param>
/// <param name="timeout"></param>
/// <param name="fileBuffer">文件数据</param>
/// <param name="fileName">文件名</param>
public static string Post(string url, byte[] fileBuffer, String fileName)
{//---------------------------1rlg4vnrodqg9f
// type=file可以上传 type=image 为不合法的文件类型
string boundary = "1rlg4vnrodqg9f"; //Microsoft.Owin.Security.Infrastructure.SecurityHelper.GenerateRadomStr();
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", string.Format("multipart/form-data; boundary={0}", boundary));
string fileFormdataTemplate =
"\r\n--" + boundary +
"\r\nContent-Disposition:form-data;name=\"{0}\";filelength=\"{2}\";filename=\"{1}\"" +
"\r\nContent-Type:application/octet-stream" +
"\r\n\r\n";
string formDataHeader = String.Format(fileFormdataTemplate, "media", fileName,fileBuffer.Length);
byte[] formDataHeaderBuffer = Encoding.UTF8.GetBytes(formDataHeader);
string begin = $"--{boundary}\r\n";
byte[] beginBuffer = Encoding.UTF8.GetBytes(begin);
string end = $"\r\n--{boundary}--\r\n";
byte[] endBuffer = Encoding.UTF8.GetBytes(end); ;
byte[] dataStream = new byte[formDataHeaderBuffer.Length + beginBuffer.Length + fileBuffer.Length + endBuffer.Length];
formDataHeaderBuffer.CopyTo(dataStream, 0);
beginBuffer.CopyTo(dataStream, formDataHeaderBuffer.Length);
fileBuffer.CopyTo(dataStream, formDataHeaderBuffer.Length + begin.Length);
endBuffer.CopyTo(dataStream, formDataHeaderBuffer.Length + begin.Length + fileBuffer.Length);
var returnBuffer = webClient.UploadData(url, "POST", dataStream);
Encoding encode = Encoding.UTF8;
string resultJson = encode.GetString(returnBuffer);
return resultJson;
}
这个是看别人例子存在的问题。。。能不能加些c#的接口例子 ,,
-------------------------
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。