| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using System;
- using System.IO;
- using System.Web;
- using System.Web.UI.WebControls;
- namespace CallCenter.Utility
- {
- /// <summary>
- /// 文件上传类
- /// </summary>
- public class FileUp
- {
- public FileUp()
- { }
- /// <summary>
- /// 转换为字节数组
- /// </summary>
- /// <param name="filename">文件名</param>
- /// <returns>字节数组</returns>
- 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];
- }
- }
- /// <summary>
- /// 流转化为字节数组
- /// </summary>
- /// <param name="theStream">流</param>
- /// <returns>字节数组</returns>
- 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();
- }
- }
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="PosPhotoUpload">控件</param>
- /// <param name="saveFileName">保存的文件名</param>
- /// <param name="imagePath">保存的文件路径</param>
- 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;
- }
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="binData">字节数组</param>
- /// <param name="fileName">文件名</param>
- /// <param name="fileType">文件类型</param>
- //-------------------调用----------------------
- //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();
- }
- }
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="hpFile">文件</param>
- /// <param name="path">路径</param>
- /// <param name="usercode">上传人</param>
- /// <returns></returns>
- public string Upload(HttpPostedFile hpFile,string path)
- {
- path = HttpContext.Current.Server.MapPath("..") + path;
- string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName;
- int iLen = hpFile.ContentLength;
- if (!Directory.Exists(path)) Directory.CreateDirectory(path);
- byte[] bData = new byte[iLen];
- hpFile.InputStream.Read(bData, 0, iLen);
- FileStream newFile = new FileStream(path + newName, FileMode.Create);
- newFile.Write(bData, 0, bData.Length);
- newFile.Flush();
- return newName;
- }
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="hpFile">文件</param>
- /// <param name="path">路径</param>
- /// <param name="usercode">上传人</param>
- /// <returns></returns>
- public string Upload(HttpPostedFile hpFile, string path, string names)
- {
- path = HttpContext.Current.Server.MapPath("..") + "/" + path;
- //string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName;
- string fileType = hpFile.FileName.Substring(hpFile.FileName.LastIndexOf("."));
- string newName = DateTime.Now.ToString("yyyyMMdd") + names + fileType;
- int iLen = hpFile.ContentLength;
- if (!Directory.Exists(path)) Directory.CreateDirectory(path);
- byte[] bData = new byte[iLen];
- hpFile.InputStream.Read(bData, 0, iLen);
- FileStream newFile = new FileStream(path + newName, FileMode.Create);
- newFile.Write(bData, 0, bData.Length);
- newFile.Flush();
- return newName;
- }
- /// <summary>
- /// 根据地址获取文件扩展名
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public string GetExt(string path)
- {
- return Path.GetExtension(path);
- }
- /// <summary>
- /// 根据路径获取文件名
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public string GetFileName(string path)
- {
- var filename = Path.GetFileName(path);
- return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + filename;
- }
- }
- }
|