using System; using System.IO; using System.Web; using System.Web.UI.WebControls; namespace Ant.Service.Utilities { /// /// 文件上传类 /// 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 fileName, string fileType) { FileStream fileStream = null; MemoryStream m = new MemoryStream(binData); try { string savePath = HttpContext.Current.Server.MapPath("~/File/"); 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(); } } } }