日期:2014-05-17 浏览次数:20982 次
/**/
/// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="fileNamePath">文件名,全路径格式 </param>
/// <param name="uriString">服务器文件夹路径 </param>
/// <param name="IsAutoRename">是否自动按照时间重命名 </param>
public void UpLoadFile(string fileNamePath, string uriString, bool IsAutoRename)
{
int indexOf = 0;
if (fileNamePath.Contains("\\"))
{
indexOf = fileNamePath.LastIndexOf("\\");
}
else if (fileNamePath.Contains("/"))
{
indexOf = fileNamePath.LastIndexOf("/");
}
string fileName = fileNamePath.Substring(indexOf + 1);
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;
//Utility.LogWriter log = new Utility.LogWriter();
//log.AddLog(uriString, "Log");
//log.AddLog(fileNamePath, "Log");
/**/
/**/
/**/
/// 创建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);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
try
{
//使用UploadFile方法可以用下面的格式