附件上传

1、将上传控件中的上传文件,转换为二进制流,以便入库

 public static byte[] RetriveUploadFileByte(HtmlInputFile iFile)
        {
            if (iFile.PostedFile.ContentLength > 0)
            {
                byte[] buffer = new byte[iFile.PostedFile.ContentLength];
                iFile.PostedFile.InputStream.Read(buffer, 0, iFile.PostedFile.ContentLength);
                return buffer;
            }
            return new byte[0];
        }

2、将上传控件中的文件,保存到服务器路径

   public static void UploadFile(HtmlInputFile iFile, string path)
        {
            if ((iFile.PostedFile != null) && (iFile.PostedFile.ContentLength > 0))
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                iFile.PostedFile.SaveAs(path + GetFileName(iFile.PostedFile.FileName));
            }
        }

  public static string GetFileName(string filePath)
        {
            if (filePath.IndexOf(@"\") == -1)
            {
                return filePath;
            }
            return filePath.Substring(filePath.LastIndexOf(@"\") + 1);
        }

  public static void UploadFile(HtmlInputFile iFile, string path, string filename)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            iFile.PostedFile.SaveAs(path + filename);
        }

3、通过字节创建文件

 public static string CreatFileByByte(string FileName, byte[] FileContent)
        {
            UrlOperate.CreatRootTempFolder();
            string str = HttpContext.Current.Server.MapPath(UrlOperate.GetApplicationPath() + "AttachStorage");
            string str2 = Guid.NewGuid().ToString();
            string path = str + @"\" + str2;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = path + @"\" + FileName;
            if (!File.Exists(path))
            {
                FileStream output = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                BinaryWriter writer = new BinaryWriter(output);
                writer.Write(FileContent);
                writer.Close();
                output.Close();
            }
            return path;
        }

 public static void WriteByteToFilePath(string strPath, byte[] bFile, bool isCover)
        {
            FileStream stream;
            string path = HttpContext.Current.Server.MapPath(strPath);
            if (isCover)
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
                stream.Write(bFile, 0, bFile.Length);
                stream.Close();
            }
            else if (!File.Exists(HttpContext.Current.Server.MapPath(path)))
            {
                stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
                stream.Write(bFile, 0, bFile.Length);
                stream.Close();
            }
        }



 public static void WriteStrToFileMapPathAppend(string strMapPath, string strContent)
        {
            byte[] bytes = Encoding.Default.GetBytes(strContent);
            FileStream stream = new FileStream(strMapPath, FileMode.Append, FileAccess.Write, FileShare.None);
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
        }

        public static void WriteStrToFilePath(string strPath, string strContent)
        {
            WriteStrToMapFilePath(HttpContext.Current.Server.MapPath(strPath), strContent);
        }

        public static void WriteStrToFilePathAppend(string strPath, string strContent)
        {
            WriteStrToFileMapPathAppend(HttpContext.Current.Server.MapPath(strPath), strContent);
        }

        public static void WriteStrToMapFilePath(string strMapPath, string strContent)
        {
            byte[] bytes = Encoding.Default.GetBytes(strContent);
            FileStream stream = new FileStream(strMapPath, FileMode.Create, FileAccess.Write, FileShare.None);
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
        }

4、检查文件类型是否可以上传

 public static bool CheckFileCanUpload(string filePre, out string AllowFileLst)
        {
           //.DOC;.DOCX; 文件类型用分号隔开
            AllowFileLst = ApplicationOperate.AllowFileLst;
            AllowFileLst = AllowFileLst.ToLower();
            if (!AllowFileLst.StartsWith(";"))
            {
                AllowFileLst = ";" + AllowFileLst;
            }
            if (!AllowFileLst.EndsWith(";"))
            {
                AllowFileLst = AllowFileLst + ";";
            }
            if (AllowFileLst.IndexOf(";" + filePre + ";") < 0)
            {
                return false;
            }
            return true;
        }

5、删除文件夹

 public static void DeleteDir(string dirPath, bool recursive)
        {
            if (Directory.Exists(dirPath))
            {
                Directory.Delete(dirPath, recursive);
            }
        }

6、删除文件

  public static void DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }

7、获取文件类型

 public static string GetFileContentType(string filename)
        {
            try
            {
                string[] strArray = filename.Split(new char[] { '.' });
                string str = string.Empty;
                string name = "." + strArray[strArray.Length - 1];
                RegistryKey key = Registry.ClassesRoot.OpenSubKey(name);
                object obj2 = key.GetValue("Content Type");
                str = (obj2 != null) ? obj2.ToString() : string.Empty;
                key.Close();
                return str;
            }
            catch (Exception exception)
            {

                return "";
            }     
        }

8、根据文件路径获取文件名

 public static string GetFileName(string filePath)
        {
            if (filePath.IndexOf(@"\") == -1)
            {
                return filePath;
            }
            return filePath.Substring(filePath.LastIndexOf(@"\") + 1);
        }

9、根据文件长度,获取文件大小(B)

 public static string getFileSize(long Length)
        {
            double num2;
            double num = Convert.ToDouble(Length);
            if (num > 1000000.0)
            {
                num2 = num / 1048576.0;
                return (num2.ToString("F") + " MB");
            }
            if (num > 1000.0)
            {
                num2 = num / 1024.0;
                return (num2.ToString("F") + " KB");
            }
            return (num + " B");
        }

10、根据文件路径,若图片则生成图片列表

 public static DataView ListFilesFromDir(string dir, string fileType)
        {
            DataTable table = new DataTable();
            table.Columns.Add("id");
            table.Columns.Add("fileName");
            table.Columns.Add("fileValue");
            table.Columns.Add("imgUrl");
            string[] files = Directory.GetFiles(HttpContext.Current.Server.MapPath(dir));
            string str = "";
            for (int i = 0; i < files.Length; i++)
            {
                str = GetFileName(files[i]).ToLower();
                if (((str.EndsWith(".gif") || str.EndsWith(".jpg")) || str.EndsWith(".bmp")) || str.EndsWith(".jpeg"))
                {
                    DataRow row = table.NewRow();
                    row["id"] = i;
                    row["fileName"] = str;
                    row["fileValue"] = str.ToLower().Replace("." + fileType.ToLower(), "");
                    row["imgUrl"] = "<img src=\"" + dir + "/" + str + "\" style=\"filter:alpha(opacity=65)\"  onMouseOver=\"flash(this,0)\" onMouseOut=\"flash(this,1)\" border=\"0\">";
                    table.Rows.Add(row);
                }
            }
            return table.DefaultView;
        }

11、根据文件路径,读取字节

  public static byte[] ReadBytesFromFilePath(string strPath)
        {
            FileStream stream = new FileStream(HttpContext.Current.Server.MapPath(strPath), FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, (int)stream.Length);
            stream.Close();
            return buffer;
        }

12、替换文件名

 public static string ReplaceFileName(string fileName)
        {
            return fileName.Replace("/", "").Replace(@"\", "").Replace(":", "").Replace("*", "").Replace("?", "").Replace("\"", "").Replace("<", "").Replace(">", "").Replace("|", "").Replace(" ", "");
        }

  public static string RetriveFileName(string filename)
        {
            filename = filename.Replace(@"\", "/").Replace("//", "/");
            if (filename.IndexOf("/") == -1)
            {
                return filename;
            }
            return filename.Substring(filename.LastIndexOf("/") + 1);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值