在mvc3中使用Telerik的upload控件上传Zip压缩文件,服务器端解压逐个上传到数据库,使用Ionic.Zip

view模板视图代码(部分)

<tr>
            <td>
                文件上传:
            </td>
            <td align="left">
                @(Html.Telerik().Upload().Name("attachments")
                .Async(async => async
                                    .Remove("Remove", "BaseInfo")
                      )
                .Multiple(false)
                   )
                   
               </td> 
        </tr>
controller中代码(部分):

 /// <summary>
        /// 编辑或者新增上传文档
        /// </summary>
        /// <param name="attachments"></param>
        [HttpPost]
        public void FormEdit(IEnumerable<HttpPostedFileBase> attachments)
        {
                 if (attachments != null)
            {
                //选择了上传文件
                foreach (var file in attachments)
                {
                    //逐个遍历zip压缩包内文件,分情况进行上传操作                    
                    Stream fstream = file.InputStream;
                    ReadOptions readOptions = new ReadOptions();
                    readOptions.Encoding = System.Text.Encoding.Default;

                    ZipFile zipFile = ZipFile.Read(fstream, readOptions);
                    //ZipFile.Read (path+"\\"+filenamet, readOptions);
                    //ZipFile zipFile = ZipFile.Read(@"C:\Users\wanglei\Desktop\testUpload.zip", readOptions);
                    IList<ZipEntry> zipList = zipFile.ToList();
                    string zipName = string.Empty;//压缩包中文件体的名字
                    List<UploadFilesDTO> listUpLoadFiles = new List<UploadFilesDTO>();
                    string tempFileName = string.Empty;//文件名
                    string tempFileCode = string.Empty;//文件编号

                    #region //遍历压缩包内的所有文件
                    foreach (ZipEntry zip in zipList)
                    {
                        string zipName=zip.FileName;//文件名
//读取当前文件流
byte[] buffer = new byte[zip.UncompressedSize];
                            var str = zip.OpenReader();
                            str.Read(buffer, 0, buffer.Length);
//下面就可以将压缩包中的当前文件名及文件流提交到数据库,具体内容不再展示
                    }
}
}
        }

针对zip压缩包的操作需要引用 Ionic.Zip;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值