using System; using System.IO; using System.Net; using System.Web; using System.Web.UI.WebControls; namespace CallCenter.Utility { /// /// 文件上传类 /// public class FileUp { public FileUp() { } /// /// 转换为字节数组 /// /// 文件名 /// 字节数组 public byte[] GetBinaryFile(string filename) { if (File.Exists(filename)) { FileStream Fsm = null; try { Fsm = File.OpenRead(filename); return this.ConvertStreamToByteBuffer(Fsm); } catch { return new byte[0]; } finally { Fsm.Close(); } } else { return new byte[0]; } } /// /// 流转化为字节数组 /// /// 流 /// 字节数组 public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream) { int bi; MemoryStream tempStream = new System.IO.MemoryStream(); try { while ((bi = theStream.ReadByte()) != -1) { tempStream.WriteByte(((byte)bi)); } return tempStream.ToArray(); } catch { return new byte[0]; } finally { tempStream.Close(); } } /// /// 上传文件 /// /// 控件 /// 保存的文件名 /// 保存的文件路径 public string FileSc(FileUpload PosPhotoUpload, string saveFileName, string imagePath) { string state = ""; if (PosPhotoUpload.HasFile) { if (PosPhotoUpload.PostedFile.ContentLength / 1024 < 10240) { string MimeType = PosPhotoUpload.PostedFile.ContentType; if (String.Equals(MimeType, "image/gif") || String.Equals(MimeType, "image/pjpeg")) { string extFileString = System.IO.Path.GetExtension(PosPhotoUpload.PostedFile.FileName); PosPhotoUpload.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(imagePath)); } else { state = "上传文件类型不正确"; } } else { state = "上传文件不能大于10M"; } } else { state = "没有上传文件"; } return state; } /// /// 上传文件 /// /// 字节数组 /// 文件名 /// 文件类型 //-------------------调用---------------------- //byte[] by = GetBinaryFile("E:\\Hello.txt"); //this.SaveFile(by,"Hello",".txt"); //--------------------------------------------- public void SaveFile(byte[] binData, string SavePath, string fileName, string fileType) { FileStream fileStream = null; MemoryStream m = new MemoryStream(binData); try { string savePath = HttpContext.Current.Server.MapPath(SavePath); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string File = savePath + fileName + fileType; fileStream = new FileStream(File, FileMode.Create); m.WriteTo(fileStream); } finally { m.Close(); fileStream.Close(); } } /// /// FTP上传文件 /// /// 需要上传的文件 /// 目标路径 /// ftp地址 /// ftp用户名 /// ftp密码 public static string UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password) { //1. check target string target; if (targetDir.Trim() == "") { return ""; } target = Guid.NewGuid().ToString(); //使用临时文件名 string URI = "FTP://" + hostname + "/" + targetDir + "/" + target; ///WebClient webcl = new WebClient(); System.Net.FtpWebRequest ftp = GetRequest(URI, username, password); //设置FTP命令 设置所要执行的FTP命令, //ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假设此处为显示指定路径下的文件列表 ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile; //指定文件传输的数据类型 ftp.UseBinary = true; ftp.UsePassive = true; //告诉ftp文件大小 ftp.ContentLength = fileinfo.Length; //缓冲大小设置为2KB const int BufferSize = 204800; byte[] content = new byte[BufferSize - 1 + 1]; int dataRead; //打开一个文件流 (System.IO.FileStream) 去读上传的文件 using (FileStream fs = fileinfo.OpenRead()) { try { //把上传的文件写入流 using (Stream rs = ftp.GetRequestStream()) { do { //每次读文件流的2KB dataRead = fs.Read(content, 0, BufferSize); rs.Write(content, 0, dataRead); } while (!(dataRead < BufferSize)); rs.Close(); } } catch (Exception ex) { } finally { fs.Close(); } } ftp = null; //设置FTP命令 ftp = GetRequest(URI, username, password); ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名 ftp.RenameTo = fileinfo.Name; try { ftp.GetResponse(); return fileinfo.Name; } catch (Exception ex) { ftp = GetRequest(URI, username, password); ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //删除 ftp.GetResponse(); throw ex; } finally { //fileinfo.Delete(); } // 可以记录一个日志 "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." ); ftp = null; #region /***** *FtpWebResponse * ****/ //FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse(); #endregion } private static FtpWebRequest GetRequest(string URI, string username, string password) { //根据服务器信息FtpWebRequest创建类的对象 FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI); //提供身份验证信息 result.Credentials = new System.Net.NetworkCredential(username, password); //设置请求完成之后是否保持到FTP服务器的控制连接,默认值为true result.KeepAlive = false; return result; } } }