110,537
社区成员
发帖
与我相关
我的任务
分享
public void UpLoadFile(string fileFullPath, string strUrlDirPath)
{
//得到要上传的文件文件名
string fileName = fileFullPath.Substring(fileFullPath.LastIndexOf("\\") + 1);
//上传路径整理
if (strUrlDirPath.EndsWith("/") == false) strUrlDirPath = strUrlDirPath + "/";
//保存在服务器上时,将文件改名(示业务需要)
strUrlDirPath = strUrlDirPath + fileName;
// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
myWebClient.Headers.Add("User-Agent", "Microsoft Internet Explorer");
// 将要上传的文件打开读进文件流
FileStream myFileStream = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read);
BinaryReader myBinaryReader = new BinaryReader(myFileStream);
//打开远程Web地址,将文件流写入
byte[] postArray = myBinaryReader.ReadBytes((int)myFileStream.Length);
myFileStream.Close();
try
{
Stream postStream = myWebClient.OpenWrite(strUrlDirPath, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Close();//关闭流
notifyIcon.ShowBalloonTip(1000, "提示", "截图上传成功", ToolTipIcon.Info);
System.Threading.Thread.Sleep(1000);
notifyIcon.Visible = false;
notifyIcon.Visible = true;
}
catch
{
notifyIcon.ShowBalloonTip(1000, "提示", "截图上传失败", ToolTipIcon.Info);
}