/// <summary> /// 文件上传 /// </summary> /// <param name="filePath">原路径(绝对路径)包括文件名</param> /// <param name="objPath">目标文件夹:服务器下的相对路径 不填为根目录</param> public static void FileUpLoad(string filePath, bool fugai, string objPath = "") { try { objPath = objPath.Replace("\\", "/"); //判断是否有该文件 FileInfo fileInfo = new FileInfo(filePath); if (FileExist(objPath, fileInfo.Name)) { if (fugai) { DeleteFile(objPath + "/" + fileInfo.Name); } else { return; } } string url = GetURI(); if (objPath != "") { url += objPath + "/"; } try { FtpWebRequest reqFTP = null; //待上传的文件 (全路径) try { //FileInfo fileInfo = new FileInfo(filePath); using (FileStream fs = fileInfo.OpenRead()) { long length = fs.Length; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + fileInfo.Name)); //设置连接到FTP的帐号密码 reqFTP.Credentials = new NetworkCredential(username, password); //设置请求完成后是否保持连接 reqFTP.KeepAlive = false; //指定执行命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; //指定数据传输类型 reqFTP.UseBinary = true; using (Stream stream = reqFTP.GetRequestStream()) { //设置缓冲大小 int BufferLength = 5120; byte[] b = new byte[BufferLength]; int i; while ((i = fs.Read(b, 0, BufferLength)) > 0) { stream.Write(b, 0, i); } Console.WriteLine("上传文件成功"); } } } catch (Exception ex) { Console.WriteLine("上传文件失败错误为" + ex.Message); } finally { } } catch (Exception ex) { Console.WriteLine("上传文件失败错误为" + ex.Message); } finally { } } catch (Exception ex) { Console.WriteLine("上传文件失败错误为" + ex.Message); } }
/// <summary> /// 上传文件 /// </summary> /// <param name="fileinfo">需要上传的文件</param> /// <param name="targetDir">目标路径文件夹</param> /// <param name="hostname">ftp地址</param> /// <param name="username">ftp用户名</param> /// <param name="password">ftp密码</param> public static void UploadFile(FileInfo fileinfo, string targetDir, bool fugai) { targetDir = targetDir.Replace("\\", "/"); //判断是否有该文件夹 CreateDirectory(targetDir); //if(DirectoryExist(targetDir,) //判断是否有该文件 if (FileExist(targetDir, fileinfo.Name)) { if (fugai) { DeleteFile(targetDir + "/" + fileinfo.Name); } else { return; } } //1. check target string target = fileinfo.Name; if (targetDir.Trim() == "") { return; } //target = Guid.NewGuid().ToString(); //使用临时文件名 string URI = GetURI() + "/" + targetDir + "/" + target; ///WebClient webcl = new WebClient(); System.Net.FtpWebRequest ftp = GetRequest(URI); //设置FTP命令 ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile; ftp.UseBinary = true; ftp.UsePassive = true; //告诉ftp文件大小 ftp.ContentLength = fileinfo.Length; const int BufferSize = 2048; byte[] content = new byte[BufferSize - 1 + 1]; int dataRead; //上传文件内容 using (FileStream fs = fileinfo.OpenRead()) { try { using (Stream rs = ftp.GetRequestStream()) { do { dataRead = fs.Read(content, 0, BufferSize); rs.Write(content, 0, dataRead); } while (!(dataRead < BufferSize)); rs.Close(); } } catch (Exception ex) { ex.ToLog(); } finally { fs.Close(); } } //ftp = null; ////设置FTP命令 //ftp = GetRequest(URI); //ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名 //ftp.RenameTo = fileinfo.Name; //try //{ // ftp.GetResponse(); //} //catch (Exception ex) //{ // ftp = GetRequest(URI); // ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //删除 // ftp.GetResponse(); // throw ex; //} //finally //{ // //fileinfo.Delete(); //} // 可以记录一个日志 "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." ); ftp = null; }