日期:2014-05-18 浏览次数:20868 次
string fileName=fileNamePath.Substring(fileNamePath.LastIndexOf("\\")+1); string NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString()+fileNamePath.Substring(fileNamePath.LastIndexOf(".")); string fileNameExt = fileNamePath.Substring(fileNamePath.LastIndexOf(".") + 1); if(uriString.EndsWith("/")==false) uriString +="/"; uriString = uriString + NewFileName; WebClient myWebCleint = new WebClient(); myWebCleint.Credentials = CredentialCache.DefaultCredentials;//获取系统凭据 //要上传的文件 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); try { byte[] postArray =r.ReadBytes((int)fs.Length); Stream postStream = myWebCleint.OpenWrite(uriString, "PUT");//写文件 progressBar1.Minimum = 0; progressBar1.Maximum = postArray.Length; if (postStream.CanWrite) { progressBar1.Value = r.Read(postArray, 0, postArray.Length); [color=#FF0000]postStream.Write(postArray, 0, postArray.Length);[/color] MessageBox.Show("文件上传成功!"); } else { MessageBox.Show("文件目前不可写"); } postStream.Close(); } catch { MessageBox.Show("文件上传失败,请重试"); }