下载中心┊文章中心┊软件分类┊更新软件┊推荐下载┊热门软件┊更新文章┊热门文章
 
《拓文asp.net网站内容管理系统》官方演示站。
 设为首页    
 收藏本站
 作者:黄振安
 
网站首页 开源拓文 .NET编程 网站编程 网页设计 图形图像 数据库 软件工程留言本
    站内搜索:   
您现在的位置: 拓文网络 → 文章中心 → .NET编程 → ASP.NET → 用 WebClient.UploadData 方法 上载文件数据[正文]

热 门 文 章

相 关 文 章
没有相关文章!
用 WebClient.UploadData 方法 上载文件数据
 
[作者:宝玉    转贴自:http://blog.joycode.com/dotey    点击数:    字体:【大 中 小】    更新时间:2006-2-24 20:53:49]


  所以只要拼一个这样的byte[] data数据Post过去,就可以达到同样的效果了。但是一定要注意,对于这种带有文件上传的,其ContentType是不一样的,例如上面的这种,其ContentType为"multipart/form-data; boundary=---------------------------7d429871607fe"。有了ContentType,我们就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我们就可以构造出我们所需要的byte[] data了,最后,不要忘记,把我们构造的ContentType传到WebClient中(例如:webClient.Headers.Add("Content-Type", ContentType);)这样,就可以通过WebClient.UploadData 方法上载文件数据了。
  
  具体代码如下:
  生成二进制数据类的封装
  
  using System;
  using System.Web;
  using System.IO;
  using System.Net;
  using System.Text;
  using System.Collections;
  
  namespace UploadData.Common
  ...{
   /**////
   /// 创建WebClient.UploadData方法所需二进制数组
   ///

   public class CreateBytes
   ...{
   Encoding encoding = Encoding.UTF8;
  
   /**////
   /// 拼接所有的二进制数组为一个数组
   ///

   /// 数组
   ///
   /// 加上结束边界
   public byte[] JoinBytes(ArrayList byteArrays)
   ...{
   int length = 0;
   int readLength = 0;
  
   // 加上结束边界
   string endBoundary = Boundary + "--\r\n"; //结束边界
   byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
   byteArrays.Add(endBoundaryBytes);
  
   foreach(byte[] b in byteArrays)
   ...{
   length += b.Length;
   }
   byte[] bytes = new byte[length];
  
   // 遍历复制
   //
   foreach(byte[] b in byteArrays)
   ...{
   b.CopyTo(bytes, readLength);
   readLength += b.Length;
   }
  
   return bytes;
}
  
   public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes)
   ...{
   WebClient webClient = new WebClient();
   webClient.Headers.Add("Content-Type", ContentType);
  
   try
   ...{
   responseBytes = webClient.UploadData(uploadUrl, bytes);
   return true;
   }
   catch (WebException ex)
   ...{
   Stream resp = ex.Response.GetResponseStream();
   responseBytes = new byte[ex.Response.ContentLength];
   resp.Read(responseBytes, 0, responseBytes.Length);
   }
   return false;
   } 
   
   
页次:2/5  1篇/页  首页   上一页   下一页   尾页  合计 5 篇 Go:
 
上一篇:没有文章
下一篇:ASP.NET Cache
winner 打印本页 关闭窗口

     文 章 评 论
评论人 查看该文章的评论 发表时间
   笔 名* : 0 50分 60分 70分 80分 90分 100分
评论内容 *: (最多600字。当前字数:0)


没有相关评论,欢迎您参与!

- 关于本站 - 网站帮助 - 下载声明 - 友情连接 - 网站地图 -
Copyright © 2005-2006 rtht.Com. All Rights Reserved . [桂ICP备06004496号]程序设计: 拓文ASP.NET网站内容管理系统 V2.6 SQL版