您的位置:首页 > 其它

七牛跨服务器上传文件带参数

2016-01-21 16:26 302 查看
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;

}
}


public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: