码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-01-21 19:15:06      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

 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         }

 

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

标签:

原文地址:http://www.cnblogs.com/qinwei/p/5148711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!