I have an image gallery and I am allowing the user to download all the images with a button. This button when pressed calls a function and a file .zip
with all the images is downloaded.
The images are saved in folders, divided into year / month / day. Small example: there is the folder 2022, inside the folder of each month (1,2,3,4,5...12), and each month folder has its day folder (01,02,03,04,05. ..31) and within the latter the images, whether they are .jpg, .png, etc.
Now what is the problem? I want only the images to be downloaded in a .zip, and not the image folders.
What I want is one .zip
called "Images Today" and that inside it only contains .jpg/.png/.jpeg (or any file that is an image). ("My images" -> .jpg)
What I do NOT want (and what is happening to me) is that the folders be downloaded. ("My images"->2022 -> 9 -> 12 -> .jpg)
This is the method I am using:
public FileResult DescargarImagenes(string pedido)
{
string consulta;
List<PedidoViewModel> listaPedidos;
byte[] bytes;
ZipFile zip;
MemoryStream output;
try
{
listaPedidos = new List<PedidoViewModel>();
consulta = "query";
listaPedidos = SelectConexionMySql(consulta);
using (zip = new ZipFile())
{
foreach (var lista in listaPedidos)
{
var archivo_nombre = lista.getPathImagen();
var archivo_arregloBytes = System.IO.File.ReadAllBytes(@"wwwroot/" + lista.getImagenes());
zip.AddEntry(archivo_nombre, archivo_arregloBytes);
}
var nombreZip = "ImagenesPedido: " + ViewBag.pedido + ".zip";
using (output = new MemoryStream())
{
zip.Save(output);
bytes = output.ToArray();
return File(bytes, "application/zip", nombreZip);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
I hope it was understood. I'm trying .jpg
to add archivo_arregloBytes
but I can't think of much more, I'm blank.
I need your help please.
I created a
String Array
callnombreImagen
, then I got the directory of the images, I made a.Split("/");
of the path and I saved it innombreImagen
, and finally to the variablearchivo_nombre
I assigned the result of position 5 ofnombreImagen
. I can do this since the directories will always be:wwwroot/imgAndroid/[Año]/[Mes]/[Dia]/[imagen].jpg/.png/.jpeg/etc
, with "[imagen].jpg/.png/.jpeg/etc
" always being position 5.