现在的位置: 首页 > 综合 > 正文

在线文件管理 for MVC

2012年12月19日 ⁄ 综合 ⁄ 共 12082字 ⁄ 字号 评论关闭

功能:支持在线浏览文件系统、下载文件、压缩并下载文件夹等;关键代码包含EasyUI Grid操作、压缩技术、MVC下载、分页等;

效果图:

 文件下载代码:

 

 public static bool DownloadFile(HttpContextBase httpContext, string filePath, long speed, string fileName)
        {
            
bool ret = true;
            
try
            {
                
#region--验证:HttpMethod,请求的文件是否存在
                
switch (httpContext.Request.HttpMethod.ToUpper())
                { 
//目前只支持GET和HEAD方法   
                    case "GET":
                    
case "HEAD":
                        
break;
                    
default:
                        httpContext.Response.StatusCode 
= 501;
                        
return false;
                }
                
if (!System.IO.File.Exists(filePath))
                {
                    httpContext.Response.StatusCode 
= 404;
                    
return false;
                }
                
#endregion

                #region 定义局部变量
                
long startBytes = 0;
                
int packSize = 1024 * 10//分块读取,每块10K bytes   
                
//string fileName = Path.GetFileName(filePath);
                FileStream myFile = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br 
= new BinaryReader(myFile);
                
long fileLength = myFile.Length;

                int sleep = (int)Math.Ceiling(1000.0 * packSize / speed);//毫秒数:读取下一数据块的时间间隔   
                string lastUpdateTiemStr = System.IO.File.GetLastWriteTimeUtc(filePath).ToString("r");
                
string eTag = HttpUtility.UrlEncode(fileName, Encoding.UTF8) + lastUpdateTiemStr;//便于恢复下载时提取请求头;   
                #endregion

                #region--验证:文件是否太大,是否是续传,且在上次被请求的日期之后是否被修
                
if (myFile.Length > Int32.MaxValue)
                {
//-------文件太大了-------   
                    httpContext.Response.StatusCode = 413;//请求实体太大   
                    return false;
                }

                if (httpContext.Request.Headers["If-Range"!= null)//对应响应头ETag:文件名+文件最后修改时间   
                {
                    
//----------上次被请求的日期之后被修改过--------------   
                    if (httpContext.Request.Headers["If-Range"].Replace("\""""!= eTag)
                    {
//文件修改过   
                        httpContext.Response.StatusCode = 412;//预处理失败   
                        return false;
                    }
                }
                
#endregion

                try
                {
                    
#region -------添加重要响应头、解析请求头、相关验证-------------------
                    httpContext.Response.Clear();
                    httpContext.Response.Buffer 
= false;
                    
//httpContext.Response.AddHeader("Content-MD5", GetMD5Hash(myFile));//用于验证文件   
                    httpContext.Response.AddHeader("Accept-Ranges""bytes");//重要:续传必须   
                    httpContext.Response.AppendHeader("ETag""\"" + eTag + "\"");//重要:续传必须   
                    httpContext.Response.AppendHeader("Last-Modified", lastUpdateTiemStr);//把最后修改日期写入响应                   
                    httpContext.Response.ContentType = "application/octet-stream";//MIME类型:匹配任意文件类型   
                    httpContext.Response.AddHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+""%20"));
                    httpContext.Response.AddHeader(
"Content-Length", (fileLength - startBytes).ToString());
                    httpContext.Response.AddHeader(
"Connection""Keep-Alive");
                    httpContext.Response.ContentEncoding 
= Encoding.UTF8;
                    
if (httpContext.Request.Headers["Range"!= null)
                    {
//------如果是续传请求,则获取续传的起始位置,即已经下载到客户端的字节数------   
                        httpContext.Response.StatusCode = 206;//重要:续传必须,表示局部范围响应。初始下载时默认为200   
                        string[] range = httpContext.Request.Headers["Range"].Split(new char[] { '=''-' });//"bytes=1474560-"   
                        startBytes = Convert.ToInt64(range[1]);//已经下载的字节数,即本次下载的开始位置     
                        if (startBytes < 0 || startBytes >= fileLength)
                        {
//无效的起始位置   
                            return false;
                        }
                    }
                    
if (startBytes > 0)
                    {
//------如果是续传请求,告诉客户端本次的开始字节数,总长度,以便客户端将续传数据追加到startBytes位置后----------   
                        httpContext.Response.AddHeader("Content-Range"string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                    }
                    
#endregion

                    #region -------向客户端发送数据块-------------------
                    br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                    
int maxCount = (int)Math.Ceiling((fileLength - startBytes + 0.0/ packSize);//分块下载,剩余部分可分成的块数   
                    for (int i = 0; i < maxCount && httpContext.Response.IsClientConnected; i++)
                    {
//客户端中断连接,则暂停   
                        httpContext.Response.BinaryWrite(br.ReadBytes(packSize));
                        httpContext.Response.Flush();
                        
if (sleep > 1) Thread.Sleep(sleep);
                    }
                    
#endregion
                }
                
catch
                {
                    ret 
= false;
                }
                
finally
                {
                    br.Close();
                    myFile.Close();
                }
            }
            
catch
            {
                ret 
= false;
            }
            
return ret;
        }  

 

 文件压缩:

 

  public static bool ZipDir(string dirPath, string password, string zipFileName)
        {
            
bool b = false;
            ZipOutputStream zos 
= null;
            
try
            {
                zos 
= new ZipOutputStream(File.Create(zipFileName));
                zos.Password 
= password;
                addZipEntry(dirPath, zos, dirPath);
                
                b 
= true;
            }
            
catch (Exception ex)
            {
                b 
= false;
            }
            
finally
            {
                
if (zos != null)
                {
                    zos.Finish();
                    zos.Close();
                }
            }
            
return b;
        }

 

分页:

 

View Code

 public class PaginatedList<T> : List<T>
    {
        
public int PageIndex { getprivate set; }
        
public int PageSize { getprivate set; }
        
public int TotalCount { getprivate set; }
        
public int TotalPages { getprivate set; }
        
public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex 
= pageIndex;
            PageSize 
= pageSize;
            TotalCount 
= source.Count();
            TotalPages 
= (int)Math.Ceiling(TotalCount / (double)PageSize);
            
this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }
        
public bool HasPreviousPage
        {
            
get
            {
                
return (PageIndex > 0);
            }
        }
        
public bool HasNextPage
        {
            
get
            {
                
return (PageIndex + 1 < TotalPages);
            }
        }
    }

 

获取列表JSON数据:

 

View Code

        public string GetList(int page, int rows, string sort, string order, string dirpath)
        {
            FileSystemItemCollection lstItem 
= new FileSystemItemCollection();
            
if (dirpath == "Home")
            {
                
string[] drivesPath = Environment.GetLogicalDrives();
                
foreach (var dictpath in drivesPath)
                {
                    FileSystemItem item 
= new FileSystemItem();
                    item.Type 
= FileSystemType.Directory;
                    DirectoryInfo dictInfo 
= new DirectoryInfo(dictpath);
                    item.Name 
= dictInfo.Name;
                    item.FullName 
= HttpUtility.UrlEncode(dictInfo.FullName);
                    item.CreateTime 
= dictInfo.CreationTime;
                    item.LastWriteTime 
= dictInfo.LastWriteTime;
                    
//item.Length = IOHelper.GetDirectoryLength(dictpath);
                    lstItem.Add(item);
                }
            }
            
else
            {
                dirpath 
= HttpUtility.UrlDecode(dirpath);
                
string webrootpath = Server.MapPath("~/");
                
if (!string.IsNullOrEmpty(dirpath))
                    webrootpath 
= dirpath;
                
// Add directorys info
                string[] dictpahts = Directory.GetDirectories(webrootpath);
                
foreach (string dictpath in dictpahts)
                {
                    FileSystemItem item 
= new FileSystemItem();
                    item.Type 
= FileSystemType.Directory;
                    DirectoryInfo dictInfo 
= new DirectoryInfo(dictpath);
                    item.Name 
= dictInfo.Name;
                    item.FullName 
= HttpUtility.UrlEncode(dictInfo.FullName);
                    item.CreateTime 
= dictInfo.CreationTime;
                    item.LastWriteTime 
= dictInfo.LastWriteTime;
                    
//item.Length = IOHelper.GetDirectoryLength(dictpath);
                    lstItem.Add(item);
                }
                
// Add files info
                string[] filepaths = Directory.GetFiles(webrootpath);
                
foreach (var filepath in filepaths)
                {
                    FileSystemItem item 
= new FileSystemItem();
                    item.Type 
= FileSystemType.File;
                    FileInfo fileInfo 
= new FileInfo(filepath);
                    item.Name 
= fileInfo.Name;
                    item.FullName 
= HttpUtility.UrlEncode(fileInfo.FullName);
                    item.CreateTime 
= fileInfo.CreationTime;
                    item.LastWriteTime 
= fileInfo.LastWriteTime;
                    item.Length 
= fileInfo.Length;
                    lstItem.Add(item);
                }
            }
            PaginatedList
<FileSystemItem> pageList = new PaginatedList<FileSystemItem>(lstItem.AsQueryable(), page-1, rows);

            string josndata = JosnHelper.ListToJson(pageList);
            
return josndata;
        }

 

EasyUI Grid代码:

 

View Code

@{
    ViewBag.Title = "Index";
}
    
<link href="@Url.Content("~/Content/easyui_themes/gray/easyui.css")" rel="stylesheet" type="text/css" />
@using (Html.BeginForm("Index", "User"))
{
<h2>Index</h2>
    
<table id="Grid"></table>
    
<div id="winViewProperty" title="View Property">
    Window Content
    
</div>
   
   @* 
<script>
    $(
'#winViewProperty').window('open');
    
</script>*@
    
<script type="text/javascript">

        $(function () {
            $(
'#Grid').datagrid({
                title: 
'File System Directory',
                url: 
'/Test/GetList?dirpath=@TempData["dirpath"]',
                singleSelect: 
true,
                
//            onLoadSuccess: function (data) { alert("adaf"); },

                
//            width: 600,
                //            height: 300,
                //                            striped: true,
                //            fitColumns: true,
                //            fit: true,
                idField: "Name",
                columns: [[
                    { field: 
'Name', title: "Name", width: 300, sortable: true },
                    { field: 
'Type', title: "Type", width: 200 },
                    { field: 
'CreateTime', title: "CreateTime", width: 200 },
                    { field: 
'LastWriteTime', title: "LastWriteTime", width: 100 },
                    { field: 
'Length', title: "Length(byte)", width: 100 },
                    { field: 
'Actions', title: 'Download', width: 100,
                        formatter: 
function (value, rowData) {
                            
var sURL = '<a href="/Test/Download?path=' + rowData.FullName + '&isFile=' + (rowData.Type == 'File'+ '">' + ((rowData.Type == 'File'? 'Download' : 'Compress Download'+ '</a>';

                            return sURL;
                        }
                    },
                    { field: 
'Actions2', title: 'Open', width: 100,
                        formatter: 
function (value, rowData) {
                            
if (rowData.Type == "File")
                                
return "";
                            
else
                                
return '<a href="/Test/Index?dirpath=' + rowData.FullName + '">Open</a>'

抱歉!评论已关闭.