附件上传
来源:互联网 发布:淘宝店铺描述在哪里看 编辑:程序博客网 时间:2024/04/29 11:09
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); }
0 0
- 上传附件
- 附件--上传
- 附件上传
- 附件上传
- 上传附件
- 附件上传
- 附件上传
- 附件上传
- C# 上传附件 删除附件
- 上传附件,附件类型问题
- typecho 不能上传附件,上传附件失败
- js 添加附件,附件上传,多附件上传
- js 添加附件,附件上传,多附件上传
- asp.net 上传附件
- asp.net 上传附件
- 怎样才能上传附件呢?
- 上传多个附件
- struts多附件上传
- MongoDB内嵌文档查询
- placeholder垂直居中(解决光标居中、文字不居中问题)
- Android旋转屏幕不销毁Activity
- Android自定义组合控件(二)
- mysql5.7安装
- 附件上传
- ASP.NET母版页使用 按下回车刷新页面 去掉 <form></form>标签
- 【C#设计模式-命令模式】
- PHP android ios相互兼容的AES加密算法
- MongoDB数组查询
- 自定义GridView,防止ScrollView中出现问题
- POJ 1789 Truck History 最小生成树
- 阿里云oss云储存错误代码403
- visual studio error:“cmd.exe” 已退出