C# 上传 下载 源代码
来源:互联网 发布:淘宝查假冒伪劣处罚 编辑:程序博客网 时间:2024/06/01 08:39
//// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="fileNamePath">文件名,全路径格式</param>
/// <param name="uriString">服务器文件夹路径</param>
private void UpLoadFile(string fileNamePath, string uriString)
{
//改名字
string NewFileName = txtMC.Text.Trim() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
string fileNameExt = fileNamePath.Substring(fileNamePath.LastIndexOf(".") + 1);
if (uriString.EndsWith("/") == false) uriString = uriString + "/";
uriString = uriString + NewFileName;
/**/
/// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
try
{
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
MessageBox.Show("上传成功!");
}
else
{
MessageBox.Show("文件目前不可写!");
}
postStream.Close();
}
catch
{
MessageBox.Show("文件上传失败,请稍候重试~");
return;
}
}
/// <summary>
/// 下载服务器文件至客户端
/// </summary>
/// <param name="URL">被下载的文件地址,绝对路径</param>
/// <param name="Dir">另存放的目录</param>
public void Download(string URL, string Dir)
{
WebClient client = new WebClient();
string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下载的文件名
string Path = Dir + fileName; //另存为的绝对路径+文件名
try
{
WebRequest myre = WebRequest.Create(URL);
}
catch
{
//MessageBox.Show(exp.Message,"Error");
}
try
{
client.DownloadFile(URL, Path);
}
catch
{
//MessageBox.Show(exp.Message,"Error");
}
}
//上传事件
private void btnsc_Click(object sender, EventArgs e)
{
if (txtMC.Text.Trim() == "")
{
MessageBox.Show("请输入文件名称!");
return;
}
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "文件类型|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtlj.Text = openFileDialog.FileName;
if (txtlj.Text.Trim() != "")
{
string a = txtlj.Text.Trim();
string c = a.Substring(a.LastIndexOf(".") + 1, 3);
if (c != "pdf")
{
MessageBox.Show("仅支持pdf文件!");
txtlj.Text = "";
return;
}
string aa = Application.StartupPath;
string bb = aa.Substring(0, aa.LastIndexOf("\\"));
string cc = bb.Substring(0, bb.LastIndexOf("\\"));
UpLoadFile(txtlj.Text.Trim(), cc + "\\ZZJH_PDF");
}
}
}
//下载事件
private void btnLode_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
txtlj.Text = folderBrowserDialog.SelectedPath;
string aa = Application.StartupPath;
string bb = aa.Substring(0, aa.LastIndexOf("\\"));
string cc = bb.Substring(0, bb.LastIndexOf("\\"));
string sc = txtlj.Text.Trim() + "\\";
string pa = txtMC.Text.Trim() + ".pdf";
Download(cc + "\\ZZJH_PDF\\" + pa + "",sc);
MessageBox.Show("下载成功!");
}
}
- C# 上传 下载 源代码
- C# Ftp 源代码(支持断点上传,断点下载)
- svn 上传源代码、下载源代码
- svn 上传源代码、下载源代码
- svn 上传源代码、下载源代码
- svn 上传源代码、下载源代码
- c#文件上传源代码
- c#大量源代码下载网址
- C# SQLHelper 源代码 源文件 下载
- [源代码]C#下载文件函数
- struts2文件上传/下载(附源代码)
- Java文件下载和上传源代码
- struts2文件上传/下载(附源代码)
- C#文件上传和下载
- C#文件上传与下载
- c# 文件上传和下载
- C# ftp 上传、下载、删除
- C# ftp上传和下载
- mule进阶之jdbc transport:
- Android中的Socket编程的基本思路
- C++ 回忆录8 copy constructor and Assigment Operator
- jrtplib 源码分析 (一) 辅助类
- sockfs: the most simple file system in linux kernel
- C# 上传 下载 源代码
- 一个教训
- 大数运算小结
- 实例repeater 分页、表头不动、添加列序号(dataset实现)
- js通过Class Name获取一个对象的数组
- xen体系结构
- 【转载】Lua 脚本语法说明
- Linux驱动调试的Debugfs的使用简介
- 使用XmlPull解析XML