Hi Everyone!
I have using model popup for selecting files for download in zip formate. Here i have used custom checkbox usind css. my line of code are given below
Checkbox code. Here checkbox are generated dynamicaly
<table>
@foreach (var item in Model)
{
<tr>
<td>
<span class="custom-checkbox">
<input id="fileItem@(item.AttachmentID)" name="fileItem@(item.AttachmentID)" type="checkbox" class="ckbox" value="1" />
<span class="box" style="background-color: #E5E5E5"><span class="tick"></span></span>
</span>
<input id="fileId" name="fileId" type="hidden" value="@item.AttachmentID" />
</td>
<td>
<label for="fileItem@(item.AttachmentID)">@Truncate(item.Description, 30)</label>
</td>
</tr>
}
</table>
<input id="btnDownload" type="submit" value="Download" />
Checkbox css
.ckbox
{
width: 25px;
height: 25px;
}
.custom-checkbox
{
position: relative;
display: inline-block;
}
.custom-checkbox > .box
{
position: relative;
display: block;
width: 25px;
height: 25px;
background-color: #E5E5E5;
padding: 0px;
margin: 0px;
}
.custom-checkbox > .box > .tick
{
position: absolute;
left: 4px;
top: 7px;
width: 14px;
height: 6px;
border-bottom: 4px solid #000;
border-left: 4px solid #000;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
transform: rotate(-45deg);
display: none;
}
.custom-checkbox > input:checked + .box > .tick
{
display: block;
}
.custom-checkbox > input
{
position: absolute;
outline: none;
left: 0;
top: 0;
padding: 0;
width: 25px;
height: 25px;
border: none;
margin: 0;
opacity: 0;
z-index: 1;
}
.td_user
{
padding: 9px 0px 5px 9px;
}
.lblFontStyle
{
font-family: TradeGothic;
font-style: oblique;
font-size: 16px;
color: #000;
}
MVC Action Code
[HttpPost]
public ActionResult AttachmentList(FormCollection formData)
{
var fileIds = formData["fileId"].Split(',');
var selectedIndices = formData["fileItem"].Replace("true,false", "true").Split(',').Select((item, index) => new
{
item = item,
index = index
}).Where(row => row.item == "true").Select(row => row.index).ToArray();
if (selectedIndices.Count() > 1)
{
using (var zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(Response.OutputStream))
{
foreach (var index in selectedIndices)
{
Response.AddHeader("Content-Disposition", "attachment; filename=ProductDescription.zip");
Response.ContentType = "application/zip";
int attachid = int.Parse(fileIds[index]);
foreach (Attachment filePath in dbZytron.Attachment.Where(m => m.AttachmentID == attachid))
{
byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(filePath.FilePath));
var fileEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(System.IO.Path.GetFileName(Server.MapPath(filePath.FilePath)))
{
Size = fileBytes.Length
};
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
}
}
zipStream.Flush();
zipStream.Close();
}
}
else
{
int attachid = int.Parse(fileIds.ElementAt(selectedIndices.ElementAt(0)));
Attachment filePath = dbZytron.Attachment.FirstOrDefault(m => m.AttachmentID == attachid);
if (filePath != null)
{
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filePath.Description + "" + ".pdf");
Response.TransmitFile(Server.MapPath(filePath.FilePath));
Response.End();
}
}
return Content(null);
}
Please help me
Thanks in advance
AVADHESH PATEL
02-Mar-2013Hi Marcel!
replace below action controll (AttachmentList) from your action controll (AttachmentList)
[HttpPost]
public ActionResult AttachmentList(FormCollection formData)
{
var fileIds = formData["fileId"].Split(',');
List<string> list = new List<string>();
foreach (var id in fileIds)
{
if (!string.IsNullOrEmpty(formData["fileItem" + id]) && Convert.ToBoolean(int.Parse(formData["fileItem" + id])))
list.Add(id);
}
if (list.Count() > 1)
{
using (var zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(Response.OutputStream))
{
foreach (var index in list)
{
Response.AddHeader("Content-Disposition", "attachment; filename=ProductDescription.zip");
Response.ContentType = "application/zip";
int attachid = int.Parse(index);
foreach (Attachment filePath in dbZytron.Attachment.Where(m => m.AttachmentID == attachid))
{
byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(filePath.FilePath));
var fileEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(System.IO.Path.GetFileName(Server.MapPath(filePath.FilePath)))
{
Size = fileBytes.Length
};
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
}
}
zipStream.Flush();
zipStream.Close();
}
}
else
{
int attachid = int.Parse(list.ElementAt(0));
Attachment filePath = dbZytron.Attachment.FirstOrDefault(m => m.AttachmentID == attachid);
if (filePath != null)
{
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filePath.Description + "" + ".pdf");
Response.TransmitFile(Server.MapPath(filePath.FilePath));
Response.End();
}
}
return Content(null);
}