1. upload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div>
<asp:FileUpload ID="SpecialsUpload" runat="server" ToolTip="浏览....."/>
<asp:Button ID="Button1" CssClass="button" ToolTip="导入专题数据" runat="server" Text="导入专题数据"
OnClick="UploadButton_Click" />
</div>
</form>
</body>
</html>
2. upload.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Win32;
using System.IO;
public partial class upload : System.Web.UI.Page
{
/// <summary>
/// 批量上传
/// </summary>
/// <param name="src"></param>
/// <param name="e"></param>
protected void UploadButton_Click(object sender, EventArgs e)
{
string path = @"C:/htmlUpload"; //上传服务器文件的存储,存在当前新建的文件夹
//判断是否有该目录
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
if (!dir.Exists)
{
dir.Create();
}
if (SpecialsUpload.HasFile)
{
//判断上传文件类型
if (System.IO.Path.GetExtension(SpecialsUpload.FileName).ToString().ToLower() == ".rar")
{
path = path + "/" + SpecialsUpload.FileName;
//如果存在,删除文件
if (File.Exists(path))
{
File.Delete(path);
}
// 上传文件
SpecialsUpload.PostedFile.SaveAs(path);
//解压缩
try
{
string zpath = @"C:/testTemp"; //解压文件存储目录
RegistryKey Reg = Registry.ClassesRoot.OpenSubKey("Applications\\WinRar.exe\\Shell\\Open\\Command");
Process process = new Process();
String rar = Reg.GetValue("").ToString();
Reg.Close();
process.StartInfo.FileName = rar.Substring(1, rar.Length - 7);
Directory.CreateDirectory(zpath);
process.StartInfo.Arguments = " X " + path + " " + zpath;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
while (!process.HasExited) //等待解压的完成
{
process.WaitForExit();
}
SpecialTopicService service = new SpecialTopicService(); //批量上传文件到数据库
service.ProcessLink(zpath + "/" + SpecialsUpload.FileName.Substring(0, SpecialsUpload.FileName.LastIndexOf(".")));
}
catch (Exception ex)
{
//Response.Write(ex.ToString());
}
}
else
{
Response.Write("<script>alert('请选择 .rar 压缩文件');window.close();</script> ");
}
}
else
{
Response.Write("<script>alert('请选择文件');window.close();</script> ");
}
}
}
3. UploadService.cs
public class UploadService : Sql.BaseService
{
/// <summary>
/// 递归文件夹获取文件
/// </summary>
/// <param name="path">文件夹路径</param>
public void ProcessLink(string path)
{
string[] files = Directory.GetFiles(path, "*.html");//获取文件夹中所有的Html文件
string[] dirList = path.Split('\\');
string id = dirList[dirList.Length - 1];
DbSQLServer.ExecuteNonQuery(strConn, "delete from topicData where id=" + id);
for (int i = 0; i < files.Length; i++)
{
//文件处理
try
{
SigleFile(files[i], i + 1);
}
catch (Exception e)
{
//cFileUtil.FileWrite("异常" + e.ToString());
}
}
string[] dirs = Directory.GetDirectories(path);
foreach (string subDir in dirs)
{
ProcessLink(subDir);
}
}
/// <summary>
/// 单个html文件处理
/// </summary>
/// <param name="file">文件名</param>
/// <param name="order">排序</param>
private void SigleFile(string file, int order)
{
FileInfo fileInfo = new FileInfo(file);
string[] dirList = fileInfo.DirectoryName.Split('\\');
string id = dirList[dirList.Length - 1];
string docname = fileInfo.Name;
StreamReader fileReader = new StreamReader(file, System.Text.Encoding.GetEncoding("utf-8"));
try
{
string strLine = "";
strLine = fileReader.ReadToEnd();
string title = Regex.Match(strLine, @"(?<=<title>)[\s\S]+?(?=</title>)").Value.Trim();
string body = Regex.Match(strLine, @"(?<=<body>)[\s\S]+?(?=</body>)").Value.Trim();
body = Regex.Replace(specialBody, @"<[\s\S]+?>", "");
string iSql = "insert into topicData(id, orderId, title,body,updateTime,flag) ";
iSql += " values ('{0}',{1},'{2}','{3}',getdate(),0)";
iSql = string.Format(iSql, id, order, title, body);
DbSQLServer.ExecuteNonQuery(strConn, iSql);
}
catch (Exception e)
{
//cFileUtil.FileWrite("异常" + e.ToString());
}
finally
{
fileReader.Close();
}
}
}