BS文件夹上传操作(二) ——基本功能实现

上篇《BS文件夹上传操作 》大概说明了我所需要的需求,

 

接着上次的命题:


 “如果有一个需求,要求你在BS上实现文件夹上传操作功能?你该如何实现?”

 

      ActiveX?Js插件?自定义控件 还是…… 

 

 

再看下需求:

        客户端选择任一文件夹上传到服务器中,在服务器上建立相关文件夹且相应文件上传到相应的文件夹中。

 

 

操作:选择任意一个文件夹上传 文件夹中相关文件也上传

 

 

出来的效果:

 

 


主要源码:

 

        /// <summary>
/// 导入->确定 方法
/// </summary>
/// <param name="path"></param>
[DirectMethod]
public void GetPath(string readPath)
{
string storePath = @"C:\Documents and Settings\Administrator\桌面\Test\"; //目标目录
string strFile = FolderHelper.FindFileByPath(readPath, storePath);
CommonMethod.Show("", strFile, Ext.Net.MessageBox.Icon.INFO);
}

 

 

FolderHelper

/// <summary>
/// 根据路径获取该文件夹下文件
/// </summary>
/// <param name="readPath">文件读取路径</param>
/// <param name="storePath">文件存储路径</param>
/// <returns>导入成功,失败</returns>
public static string FindFileByPath(string readPath, string storePath)
{
try
{
DirectoryInfo diInfo = new DirectoryInfo(readPath); //上传
DirectoryInfo diInfoStore = new DirectoryInfo(storePath + diInfo.Name);//存储
if (!diInfoStore.Exists)//存储到指定路径判断文件夹不存在则新建
diInfoStore.Create();
FileSystemInfo[] fsInfo = diInfo.GetFileSystemInfos();//获取该目录下所有文件和子目录
foreach (FileSystemInfo fs in fsInfo)//遍历数组
{
if (fs is FileInfo)//文件
{
byte[] file = FileToBinary(fs.FullName);
BinaryToFile(storePath + diInfo.Name, fs.Name, file);
}
if (fs is DirectoryInfo)//文件夹
{
FindFileByPath(fs.FullName, storePath + diInfo.Name + "\\");
}
}
return "导入成功";
}
catch (Exception)
{
return "导入失败";
}
}
/// <summary>
/// 将文件转换为二进制流进行读取
/// </summary>
/// <param name="fileName">文件完整名路径</param>
/// <returns>文件二进制流</returns>
private static byte[] FileToBinary(string fileName)
{
FileStream fsRead = new FileStream(fileName, FileMode.Open, FileAccess.Read);
try
{
if (fsRead.CanRead)
{
int fsSize = Convert.ToInt32(fsRead.Length);
byte[] btRead = new byte[fsSize];
fsRead.Read(btRead, 0, fsSize);
return btRead;
}
else
{
throw new Exception("文件读取错误!");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
fsRead.Close();
}
}
/// <summary>
/// 将二进制流转换为对应的文件进行存储
/// </summary>
/// <param name="storePath">存储文件名的路径</param>
/// <param name="fileName">文件名</param>
/// <param name="btBinary">二进制流</param>
private static void BinaryToFile(string storePath, string fileName, byte[] binary)
{
FileStream fsWrite = new FileStream(storePath + "\\" + fileName, FileMode.Create, FileAccess.Write);
try
{
if (fsWrite.CanWrite)
{
fsWrite.Write(binary, 0, binary.Length);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
fsWrite.Close();
}
}


 基本的上传功能实现,但是牺牲了安全性,抽空试试Left join.G 提供的帮助ActiveX。在这也感谢园中两位朋友的Left join.G和sunriseyuen。


谢谢

 

 

 

 

 

 

 


作者:PEPE
出处:http://pepe.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2012-02-15 22:34  PEPE YU  阅读(2247)  评论(9编辑  收藏  举报