/////// WebClient上传文件至服务器 ///
/// 文件名,全路径格式
/// 服务器文件夹路径
private void UpLoadFile(string fileNamePath,string uriString)
{
//string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
string fileNameExt = fileName.Substring(fileName.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);
}
else
{
MessageBox.Show("文件目前不可写!");
} postStream.Close();
}
catch
{
MessageBox.Show("文件上传失败,请稍候重试~");
}
}
/// 下载服务器文件至客户端
/// 被下载的文件地址,绝对路径
/// 另存放的目录
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");
}
} ///
/// 下载文件
////// 网址
/// 文件名
/// 进度条
public static void DownFile( string URL, string Filename, ProgressBar Prog )
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
//从URL地址得到一个WEB请求
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
//从WEB请求得到WEB响应
long totalBytes = myrp.ContentLength;
//从WEB响应得到总字节数
Prog.Maximum = (int)totalBytes;
//从总字节数得到进度条的最大值
System.IO.Stream st = myrp.GetResponseStream();
//从WEB请求创建流(读)
System.IO.Stream so = new System.IO.FileStream(Filename, System.IO.FileMode.Create);
//创建文件流(写)
long totalDownloadedByte = 0;
//下载文件大小
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
//读流
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
//更新文件大小
Application.DoEvents(); so.Write(by, 0, osize);
//写流
Prog.Value = (int)totalDownloadedByte;
//更新进度条
osize = st.Read(by, 0, (int)by.Length);
//读流
}
//关闭流
st.Close();
}
/// 文件名,全路径格式
/// 服务器文件夹路径
private void UpLoadFile(string fileNamePath,string uriString)
{
//string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
string fileNameExt = fileName.Substring(fileName.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);
}
else
{
MessageBox.Show("文件目前不可写!");
} postStream.Close();
}
catch
{
MessageBox.Show("文件上传失败,请稍候重试~");
}
}
/// 下载服务器文件至客户端
/// 被下载的文件地址,绝对路径
/// 另存放的目录
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");
}
} ///
/// 下载文件
////// 网址
/// 文件名
/// 进度条
public static void DownFile( string URL, string Filename, ProgressBar Prog )
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
//从URL地址得到一个WEB请求
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
//从WEB请求得到WEB响应
long totalBytes = myrp.ContentLength;
//从WEB响应得到总字节数
Prog.Maximum = (int)totalBytes;
//从总字节数得到进度条的最大值
System.IO.Stream st = myrp.GetResponseStream();
//从WEB请求创建流(读)
System.IO.Stream so = new System.IO.FileStream(Filename, System.IO.FileMode.Create);
//创建文件流(写)
long totalDownloadedByte = 0;
//下载文件大小
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
//读流
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
//更新文件大小
Application.DoEvents(); so.Write(by, 0, osize);
//写流
Prog.Value = (int)totalDownloadedByte;
//更新进度条
osize = st.Read(by, 0, (int)by.Length);
//读流
}
//关闭流
st.Close();
}