winform怎么上传文件到服务器,能否发送一段代码,初学者,相当感谢~
3个回答
2011-04-01
展开全部
// 得到文件名,文件扩展名字,服务器路径
Uri = Uri + _contractid + "_" + _filename;
//访问权限设置
c = new WebClient();
c.Credentials = CredentialCache.DefaultCredentials;
//c.UploadFile(Uri,"PUT", _resultFile);
//// 要上传的文件
FileStream fs = new FileStream(_resultFile, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
try
{
byte[] postArray = br.ReadBytes((int)fs.Length);
Stream postStream = c.OpenWrite(Uri, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
lb_state.Text = _filename + "上传成功!";
}
else
{
lb_state.Text = "文件目前不可写!";
}
postStream.Close();
this._returnvalue = true;
}
catch (WebException errMsg)
{
lb_state.Text = "上传失败:" + errMsg.Message;
}
Uri = Uri + _contractid + "_" + _filename;
//访问权限设置
c = new WebClient();
c.Credentials = CredentialCache.DefaultCredentials;
//c.UploadFile(Uri,"PUT", _resultFile);
//// 要上传的文件
FileStream fs = new FileStream(_resultFile, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
try
{
byte[] postArray = br.ReadBytes((int)fs.Length);
Stream postStream = c.OpenWrite(Uri, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
lb_state.Text = _filename + "上传成功!";
}
else
{
lb_state.Text = "文件目前不可写!";
}
postStream.Close();
this._returnvalue = true;
}
catch (WebException errMsg)
{
lb_state.Text = "上传失败:" + errMsg.Message;
}
展开全部
看看这个,希望对你有用
参考资料: http://blog.sina.com.cn/s/blog_3fed3a390100g1pr.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询