Skip to content

Instantly share code, notes, and snippets.

@donma
Created January 24, 2019 07:23
Show Gist options
  • Save donma/8cb83044358e00f81a807bc94465337b to your computer and use it in GitHub Desktop.
Save donma/8cb83044358e00f81a807bc94465337b to your computer and use it in GitHub Desktop.
private string UploadImage(string file)
{
var src = System.IO.File.ReadAllBytes(file);
Stream stream = new MemoryStream(src);
HttpContent fileStreamContent = new StreamContent(stream);
fileStreamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "file", FileName = "xxx.jpg" };
fileStreamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(new StringContent("your_token"), "token");
formData.Add(new StringContent("測試"), "tag");
// 如果你指定filename 他就會覆蓋原本的圖片
// formData.Add(new StringContent("testgif"), "filename");
formData.Add(fileStreamContent, "file");
// Remember change your domain to https://yourdomain.com/api/upload to upload image.
var response = client.PostAsync("https://yourdomain.com/api/upload", formData).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
var result = UploadImage(AppDomain.CurrentDomain.BaseDirectory + "iphone7.jpg");
//response success
//success:imageid
//sample
//success:01d1z45bm0nw8r4hfqcr6zbv26
//response error
//error:error result
//sample
//error:token null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment