在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;

你可能感兴趣的:(在mvc3中使用Telerik的upload控件上传Zip压缩文件,服务器端解压逐个上传到数据库,使用Ionic.Zip)