asp.net上传压缩文件
来源:互联网 发布:昆仑数据科技面试 编辑:程序博客网 时间:2024/04/30 08:50
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();
}
}
}
- asp.net上传压缩文件
- 如何解决Asp.Net中不能上传压缩文件的问题
- Asp.net压缩文件
- ASP.NET 压缩和解压缩文件
- asp.net调用WinRAR来压缩文件
- vs2003 asp.net服务端压缩文件并下载
- ASP.NET生成压缩文件(rar打包)
- ASP.NET生成压缩文件(rar打包)
- .NET压缩文件
- asp.net文件上传
- asp.net上传图片
- ASP.Net文件上传
- ASP.Net文件上传
- asp.net 上传附件
- asp.net 上传附件
- ASP.NET上传文件
- asp.net文件上传
- asp.net上传图片
- Java关键字final、static使用总结
- 【BZOJ】【P2819】【Nim】【题解】【链剖线段树】
- sicily_course_1001
- 学习
- 2014 ACM/ICPC 牡丹江赛区网络赛解题报告汇总
- asp.net上传压缩文件
- 关于报错you cannot combine custom titles with other title features
- Error building Player: Win32Exception: ApplicationName='E:/adt-20140702/sdk\tools\zipalign.exe', Com
- openstack页面自定义插件使用详解(django、ajax、post)(zTree为例)
- UDP中的Connect
- 随想
- meta辅助性标签。
- jquery validate 详解一
- Xcode 6 bate2的swift的半闭区间新语法说明