七牛跨服务器上传文件带参数
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; }
相关文章推荐
- c# 备份数据库恢复数据库
- 升级Win10 11099、11082补丁卡住不动问题的两种解决方法
- 我眼中多线程的锁
- Python join和split函数
- SAP快速找到定制配置
- 链表常见面试题四:解决链表相交问题
- 远程连接linux执行命并且返回执行结果
- sp<>,wp<>
- RabbitMQ 入门 Helloworld
- C#调用FFMPEG实现桌面录制(视频+音频+生成本地文件)【笔记】
- Java学习第9天(2):面向对象-多态-子父类类型变换
- unity3d 播放视频的方法总结
- 解决:dubbo找不到dubbo.xsd报错
- 利用Console来调试JS程序、Console用法总结
- 如何将文章列表用<li>分两列显示
- 多线程单利模式之双检锁必要性
- Quartz.net持久化与集群部署开发详解
- cocos2dx 3.9 创建自定义mesh进行渲染。
- AWS 之 S3篇<.NET(c#)批量上传文件>
- 安卓测试