資源簡介
以下程序在.NET2005,系統XP中測試通過:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace TQSystem.Com
{
class UpDownLoadFile
{
/// <summary>
/// WebClient上傳文件至服務器(不帶進度條)
/// </summary>
/// <param name="fileNameFullPath">要上傳的文件(全路徑格式)</param>
/// <param name="strUrlDirPath">Web服務器文件夾路徑</param>
/// <returns>True/False是否上傳成功</returns>
public bool UpLoadFile(string fileNameFullPath, string strUrlDirPath)
{
//得到要上傳的文件文件名
string fileName = fileNameFullPath.Substring(fileNameFullPath.LastIndexOf("\\") 1);
//新文件名由年月日時分秒及毫秒組成
string NewFileName = DateTime.Now.ToString("yyyyMMddhhmmss")
DateTime.Now.Millisecond.ToString()
fileNameFullPath.Substring(fileNameFullPath.LastIndexOf("."));
//得到文件擴展名
string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") 1);
if (strUrlDirPath.EndsWith("/") == false) strUrlDirPath = strUrlDirPath "/";
//保存在服務器上時,將文件改名(示業務需要)
strUrlDirPath = strUrlDirPath NewFileName;
// 創建WebClient實例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 將要上傳的文件打開讀進文件流
FileStream myFileStream = new FileStream(fileNameFullPath, FileMode.Open, FileAccess.Read);
BinaryReader myBinaryReader = new BinaryReader(myFileStream);
try
{
byte[] postArray = myBinaryReader.ReadBytes((int)myFileStream.Length);
//打開遠程Web地址,將文件流寫入
Stream postStream = myWebClient.OpenWrite(strUrlDirPath, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
//MessageBox.Show("Web服務器文件目前不可寫入,請檢查Web服務器目錄權限設置!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
postStream.Close();//關閉流
return true;
}
catch (Exception exp)
{
//MessageBox.Show("文件上傳失敗:" exp.Message, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}
/// <summary>
/// 下載服務器文件至客戶端(不帶進度條)
/// </summary>
/// <param name="strUrlFilePath">要下載的Web服務器上的文件地址(全路徑 如:http://www.dzbsoft.com/test.rar)</param>
/// <param name="Dir">下載到的目錄(存放位置,機地機器文件夾)</param>
/// <returns>True/False是否上傳成功</returns>
public bool DownLoadFile(string strUrlFilePath, string strLocalDirPath)
{
// 創建WebClient實例
WebClient client = new WebClient();
//被下載的文件名
string fileName = strUrlFilePath.Substring(strUrlFilePath.LastIndexOf("/"));
//另存為的絕對路徑+文件名
string Path = strLocalDirPath fileName;
try
{
WebRequest myWebRequest = WebRequest.Create(strUrlFilePath);
}
catch (Exception exp)
{
MessageBox.Show("文件下載失敗:" exp.Message, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
client.DownloadFile(strUrlFilePath, Path);
return true;
}
catch (Exception exp)
{
MessageBox.Show("文件下載失敗:" exp.Message, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}
/// <summary>
/// 下載帶進度條代碼(普通進度條)
/// </summary>
/// <param name="URL">網址</param>
/// <param name="Filename">文件名</param>
/// <param name="Prog">普通進度條ProgressBar</param>
/// <returns>True/False是否下載成功</returns>
public bool DownLoadFile(string URL, string Filename, ProgressBar Prog)
{
try
{
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); //讀流
}
so.Close(); //關閉流
st.Close(); //關閉流
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 下載帶進度條代碼(狀態欄式進度條)
/// </summary>
/// <param name="URL">網址</param>
/// <param name="Filename">文件名</param>
/// <param name="Prog">狀態欄式進度條ToolStripProgressBar</param>
/// <returns>True/False是否下載成功</returns>
public bool DownLoadFile(string URL, string Filename, ToolStripProgressBar Prog)
{
try
{
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); //讀流
}
so.Close(); //關閉流
st.Close(); //關閉流
return true;
}
catch
{
return false;
}
}
}
}
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Text;
namespace?Stl_UpLoadFile
{
????class?ButtonState
????{
????????public?const?string?BTN_UP?=?“上傳“;
????????public?const?string?BTN_STOP?=?“停止“;
????????public?const?string?BTN_CANCEL?=?“取消“;
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????52736??2012-04-05?17:37??WinFrom上傳文件\WinFrom通過WebClient上傳下載文件.doc
?????文件??????36864??2009-03-31?18:32??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Debug\Stl_UpLoadFile.exe
?????文件??????73216??2009-03-31?18:32??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Debug\Stl_UpLoadFile.pdb
?????文件???????5632??2005-11-11?22:25??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Debug\Stl_UpLoadFile.vshost.exe
?????文件???????3263??2009-03-31?18:34??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Debug\UploadFile.xm
?????文件??????24064??2012-04-11?18:52??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Release\Stl_UpLoadFile.exe
?????文件??????52736??2012-04-11?18:52??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Release\Stl_UpLoadFile.pdb
?????文件??????14328??2012-04-11?19:07??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Release\Stl_UpLoadFile.vshost.exe
?????文件????????490??2007-07-21?02:33??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Release\Stl_UpLoadFile.vshost.exe.manifest
?????文件???????2228??2012-04-11?18:53??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\bin\Release\UploadFile.xm
?????文件????????285??2008-12-09?17:28??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\ButtonState.cs
?????文件????????570??2009-03-31?17:47??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\ChangeIP.cs
?????文件???????4037??2009-03-31?17:46??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\ChangeIP.Designer.cs
?????文件???????5814??2008-12-09?17:28??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\ChangeIP.resx
?????文件????????878??2008-12-09?17:28??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\FileData.cs
?????文件???????2530??2008-12-09?17:28??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\FileState.cs
?????文件????????180??2009-03-31?17:43??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Debug\Stl_UpLoadFile.ChangeIP.resources
?????文件????????908??2009-03-31?17:43??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Debug\Stl_UpLoadFile.csproj.GenerateResource.Cache
?????文件??????36864??2009-03-31?18:32??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Debug\Stl_UpLoadFile.exe
?????文件??????73216??2009-03-31?18:32??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Debug\Stl_UpLoadFile.pdb
?????文件????????180??2009-03-31?17:43??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Debug\Stl_UpLoadFile.Properties.Resources.resources
?????文件????????180??2009-03-31?17:43??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Debug\Stl_UpLoadFile.UpLoadFile.resources
?????文件????????180??2012-04-11?18:21??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Release\Stl_UpLoadFile.ChangeIP.resources
?????文件???????1294??2012-04-11?19:07??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Release\Stl_UpLoadFile.csproj.FileListAbsolute.txt
?????文件????????913??2012-04-11?18:23??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Release\Stl_UpLoadFile.csproj.GenerateResource.Cache
?????文件??????24064??2012-04-11?18:52??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Release\Stl_UpLoadFile.exe
?????文件??????52736??2012-04-11?18:52??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Release\Stl_UpLoadFile.pdb
?????文件????????180??2012-04-11?18:21??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Release\Stl_UpLoadFile.Properties.Resources.resources
?????文件????????180??2012-04-11?18:21??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Release\Stl_UpLoadFile.UpLoadFile.resources
?????文件???????4608??2012-04-11?18:22??WinFrom上傳文件\支持大文件的C#文件上傳源碼\Stl_UpLoadFile\obj\Release\TempPE\Properties.Resources.Designer.cs.dll
............此處省略43個文件信息
評論
共有 條評論