《拓文asp.net网站内容管理系统》官方演示站。
设为首页
收藏本站
作者:黄振安
QQ:53854651
首 页
开源拓文
.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;
}
首页
|
1
|
2
|
3
|
4
|
5
|
尾页
共
5
页
winner
打印本页
关闭窗口
文 章 评 论
评论人
查看该文章的评论
发表时间
笔 名* :
0
50分
60分
70分
80分
90分
100分
评论内容 *: (最多1000字。当前字数:
0
)
80 分
123
分非凡非凡非凡非凡非凡非凡非凡方法
2006-4-10 21:58:10
50 分
hello,laoK
2006-3-6 12:13:44
- 关于本站 - 网站帮助 - 下载声明 - 友情连接 - 网站地图 -
Copyright © 2005-2006
myTopWin.Com
. All Rights Reserved .
[桂ICP备06004496号]