上传
string FilePath = AppDomain.CurrentDomain.BaseDirectory + "DownLoad/";
UpLoadFile(txtCLPath.Text, FilePath, false);
/// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="fileNamePath">文件名,全路径格式</param
/// <param name="uriString">服务器文件夹路径</param>
/// <param name="IsAutoRename">是否自动按照时间重命名</param>
public void UpLoadFile(string fileNamePath, string uriString, bool IsAutoRename)
{
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
//上传的文件样式xxxxxxxxx_006.xlsx
//string strVN = NewFileName.Substring(NewFileName.LastIndexOf("_") + 1);//"006.xlsx"
//strVN.Substring(0, strVN.LastIndexOf("."));//006
//NewFileName.Substring(0, NewFileName.LastIndexOf("_"))//xxxxxxxxx
string NewFileName = fileName;
if (IsAutoRename)
{
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 myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
try
{
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",fileNamePath);
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
postStream.Close();
fs.Dispose();
}
else
{
postStream.Close();
fs.Dispose();
}
}
catch
{
postStream.Close();
fs.Dispose();
}
finally
{
postStream.Close();
fs.Dispose();
}
}
下载
string FilePath = AppDomain.CurrentDomain.BaseDirectory + "DownLoad/";
string strDownUrl = System.IO.Path.GetFullPath(FilePath + cmbName1.SelectedValue + "_" + cmbBanbenHao.SelectedValue + ".xlsx");
if (System.IO.File.Exists(strDownUrl)) //判断文件是否存在
{
//ConfigurationManager.AppSettings["Downpath"] app.config维护的下载路径
//<add key="Downpath" value="d:\SP\DownLoad" />
Download(strDownUrl, ConfigurationManager.AppSettings["Downpath"] + "\\");
}
/// <summary>
/// 下载服务器文件至客户端
/// </summary>
/// <param name="URL">被下载的文件地址,绝对路径</param>
/// <param name="Dir">另存放的目录</param>
public void Download(string URL, string Dir)
{
if (!Directory.Exists(Dir))
{
Directory.CreateDirectory(Dir);
}
string fileNamePath = URL.Substring(URL.LastIndexOf("DownLoad")); //DownLoad\被下载的文件名;
string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下载的文件名
string Path = Dir + fileName; //另存为的绝对路径+文件名
try
{
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] mbyte = r.ReadBytes((int)fs.Length);
FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(mbyte, 0, (int)fs.Length);
fs.Close();
fstr.Close();
MessageBox.Show("成功下载至D:/SP/DownLoad", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
fs.Close();
fstr.Close();
}
}