3.diskHelper类
Posted feihusurfer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.diskHelper类相关的知识,希望对你有一定的参考价值。
//文件和目录操作辅助类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Data;
namespace rui
{
public class diskHelper
{
//创建目录
public static void createDirectory(string path)
{
path = HttpContext.Current.Server.MapPath(path);
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
}
//删除目录
public static void deleteDirectory(string path)
{
path = HttpContext.Current.Server.MapPath(path);
Directory.Delete(path, true);
}
//删除文件
public static void deleteFile(string path)
{
path = HttpContext.Current.Server.MapPath(path);
if (File.Exists(path))
File.Delete(path);
}
//文件是否存在
public static bool isExist(string path)
{
path = HttpContext.Current.Server.MapPath(path);
return File.Exists(path);
}
//获取目录下所有的文件(按照图片的顺序获取图片文件)------------------------------这个方法还没看!!!
public static List<string> getFiles(string resultPath, string designCode)
{
using (rui.dbHelper dbHelper = rui.dbHelper.createHelper(null))
{
resultPath = HttpContext.Current.Server.MapPath(resultPath);
string sql = @" SELECT CONVERT(NVARCHAR(100),orderNo)+‘_‘+pictureCode+‘.jpg‘ AS filename FROM dbo.CusDesignPic WHERE designCode=‘" + designCode + "‘ ORDER BY orderNo ";
DataTable table = dbHelper.ExecuteReaderDataTable(sql);
List<string> list = new List<string>();
foreach (DataRow row in table.Rows)
{
list.Add(string.Format("{0}/{1}", resultPath, row["filename"].ToString()));
}
return list;
}
}
//获取客户端上传的文件名
public static string getUploadFileName(string filename)
{
//去除文件路径
int index = filename.LastIndexOf("\\");
if (index > 0)
filename = filename.Substring(index + 1);
//去除浏览器不支持的#符号
filename = filename.Replace("#", "_");
return filename;
}
//上传文件
/// <summary>
///
/// </summary>
/// <param name="savePath">上传文件所保存的位置</param>
/// <returns>返回上传文件的文件名称和存储路径</returns>
public static List<string> uploadFiles(string savePath, string nameKey)
{
List<string> list = new List<string>();
//获取附件并保存
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int i = 0; i < files.Count; i++)
{
if (files.AllKeys[i] == nameKey)
{
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + rui.diskHelper.getUploadFileName(files[i].FileName);
string filepath = string.Format("{0}/{1}", savePath, filename);
string PhysicalPath = HttpContext.Current.Server.MapPath("~" + filepath);
files[0].SaveAs(PhysicalPath);
list.Add(filepath);
}
}
return list;
}
}
}
以上是关于3.diskHelper类的主要内容,如果未能解决你的问题,请参考以下文章