日期:2014-05-18 浏览次数:20503 次
/// <summary> /// 上传图片 c# /// </summary> /// <param name= "fileNamePath "> 文件路径 </param> /// <param name= "uriString "> 保存路径 </param> /// using System.IO; /// using System.Net; private void UpLoadFile(string fileNamePath,string uriString) { string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf( "\\ ") + 1); string 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实例 WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件 // FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); //FileStream fs = OpenFile(); BinaryReader r = new BinaryReader(fs); // try // { //使用UploadFile方法可以用下面的格式 //myWebClient.UploadFile(uriString, "PUT ",fileNamePath); byte[] postArray = r.ReadBytes((int)fs.Length); Stream postStream = myWebClient.OpenWrite(uriString, "PUT "); if(postStream.CanWrite) { postStream.Write(postArray,0,postArray.Length); } else { Response.Write( "文件目前不可写! "); } postStream.Close(); }