[c#]代码库
/**/
/// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="fileNamePath">文件名,全路径格式</param>
/// <param name="uriString">服务器文件夹路径</param>
/// <param name="IsAutoRename">是否自动按照时间重命名</param>
public void UpLoadFile(string fileNamePath, string uriString)
{
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
string NewFileName = fileName;
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方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",fileNamePath);
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
postStream.Close();
fs.Dispose();
// log.AddLog("上传日志文件成功!", "Log");
}
else
{
postStream.Close();
fs.Dispose();
// log.AddLog("上传日志文件失败,文件不可写!", "Log");
}
}
catch (Exception err)
{
postStream.Close();
fs.Dispose();
//Utility.LogWriter log = new Utility.LogWriter();
// log.AddLog(err, "上传日志文件异常!", "Log");
throw err;
}
finally
{
postStream.Close();
fs.Dispose();
}
}
by: 发表于:2018-01-12 14:33:07 顶(0) | 踩(0) 回复
??
回复评论