HttpPostedFileBase file = Request.Files["file"];
//System.IO.Stream s = file.InputStream;
byte[] buffer = new byte[1024];
//int bytesRead = 0;
//while ((bytesRead = file.InputStream.Read(buffer, 0, buffer.Length)) != 0)
//{
//}
buffer=StreamToBytes(file.InputStream);
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jpeg"));//设定要响应的数据格式
using (var content = new MultipartFormDataContent())//表明是通过multipart/form-data的方式上传数据
{
// content.Add(new ByteArrayContent(buffer, 0, buffer.Count()), "file", "aaasf.jpg");
var policy = new Qiniu.RS.PutPolicy("file", 3600);
// content.Add(new StringContent("0"), "detectMime");
content.Add(new StringContent(Guid.NewGuid().ToString()), "key");
content.Add(new StringContent("file"), "bucket");
var imageContent = new ByteArrayContent(buffer, 0, buffer.Count());
imageContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
content.Add(imageContent, "file", file.FileName);
content.Add(new StringContent(policy.Token()), "token");
// content.Add(new StringContent("image/jpeg"), "Accept");
var result = httpClient.PostAsync("http://up.qiniu.com", content).Result.Content.ReadAsStringAsync().Result;
}
}
1 public byte[] StreamToBytes(Stream stream)
2 {
3 byte[] bytes = new byte[stream.Length];
4 stream.Read(bytes, 0, bytes.Length);
5 // 设置当前流的位置为流的开始
6 stream.Seek(0, SeekOrigin.Begin);
7 return bytes;
8 }