zoukankan      html  css  js  c++  java
  • 七牛跨服务器上传文件带参数

     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         }
  • 相关阅读:
    Python入门--14--字典
    Python入门--13--爬虫一
    Python入门--13--递归
    Python入门--12--函数与变量
    Python入门--11--自定义函数
    Python入门--10--序列
    mysql 删除重复记录
    Java 不可编辑的Map
    mysql left join
    mysql 超过5名学生的课
  • 原文地址:https://www.cnblogs.com/qinwei/p/5148711.html
Copyright © 2011-2022 走看看