Question:
I have the following code which I use to register images and pdf's, the problem is that until now when I register for example two different images it saves me 1 but 2 times. And in the case of the PDF what I want is that I save only one for the images that I upload. When registering them, I save the URL of both the images and the pdf.
The <input>
I use for images is a multiple
.
public ActionResult Insertar_Datos(string Pais, string fecAct, string fecVen, string tituloLanzamiento, string nombreBanner, HttpPostedFileBase urlImg, HttpPostedFileBase urlPdf)
{
for(int i = 0; i<Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i];
if (file.ContentLength > 0)
{
string ruta = Request.MapPath("Imagenes_Landing/");
Random r = new Random();
int numero = r.Next(5, 10000000);
nombreBanner = nombreBanner.Replace(" ", "_");
if (Directory.Exists(ruta) == false)
Directory.CreateDirectory(ruta);
string archivo = Path.GetFileName(urlImg.FileName);
string extension = Path.GetExtension(archivo);
archivo = archivo.Substring(archivo.LastIndexOf(".") + 1).ToLower();
archivo = "Imagen_" + numero + '_' + Pais + extension;
urlImg.SaveAs(ruta + archivo);
string urlImgen = "/Home/Imagenes_Landing/" + archivo;
nombreBanner = nombreBanner.Replace("_", " ");
///////////// GUARDADO DE PDFs
string rutaPDF = Request.MapPath("PDFs_Landing/");
Random ra = new Random();
int numeroP = r.Next(5, 10000000);
tituloLanzamiento = tituloLanzamiento.Replace(" ", "_");
if (Directory.Exists(rutaPDF) == false)
Directory.CreateDirectory(rutaPDF);
string documento = Path.GetFileName(urlPdf.FileName);
string extensionP = Path.GetExtension(documento);
documento = documento.Substring(documento.LastIndexOf(".") + 1).ToLower();
documento = "Documento_" + numeroP + '_' + Pais + extensionP;
string urlPDF = "/Home/PDFs_Landing/" + documento;
urlPdf.SaveAs(rutaPDF + documento);
nombreBanner = nombreBanner.Replace("_", " ");
ViewBag.Message = ManejoDatos.AgregarRegistro_LandingPage("Amairani Fernanda Rodriguez", Pais, fecAct, fecVen, tituloLanzamiento, nombreBanner, urlImgen, urlPDF );
}
}
return RedirectToAction("NuevosLanzamientos", "Home");
}
What I am looking for is that if I upload 4 images, they are all different, not the same and with the pdf that I do not duplicate it but it is 1 pdf for the 4 images
If someone could help me, I would be very grateful: D
Answer:
In case someone ever needs it, it was resolved as follows:
In the controller a method was created to register the PDF and it was called in my method where the images are registered.
[HttpPost]
public string InsertarPDF(HttpPostedFileBase urlPdf, string tituloLanzamiento, string Pais)
{
string rutaPDF = Request.MapPath("PDFs_Landing/");
Random ra = new Random();
int numeroP = ra.Next(5, 10000000);
tituloLanzamiento = tituloLanzamiento.Replace(" ", "_");
if (Directory.Exists(rutaPDF) == false)
Directory.CreateDirectory(rutaPDF);
string documento = Path.GetFileName(urlPdf.FileName);
string extensionP = Path.GetExtension(documento);
documento = documento.Substring(documento.LastIndexOf(".") + 1).ToLower();
documento = "Documento_" + numeroP + '_' + Pais + extensionP;
string urlPDF = "/Home/PDFs_Landing/" + documento;
urlPdf.SaveAs(rutaPDF + documento);
//nombreBanner = nombreBanner.Replace("_", " ");
return urlPDF;
}
public ActionResult Insertar_Datos(string Pais, string fecAct, string fecVen, string tituloLanzamiento,
IEnumerable<HttpPostedFileBase> urlImg, HttpPostedFileBase urlPdf)
{
string urlPDF = InsertarPDF(urlPdf, tituloLanzamiento, Pais);
foreach(HttpPostedFileBase file in urlImg)
{
string ruta = Request.MapPath("Imagenes_Landing/");
Random r = new Random();
int numero = r.Next(5, 10000000);
//nombreBanner = nombreBanner.Replace(" ", "_");
if (Directory.Exists(ruta) == false)
Directory.CreateDirectory(ruta);
string archivo = Path.GetFileName(file.FileName);
string extension = Path.GetExtension(archivo);
archivo = archivo.Substring(archivo.LastIndexOf(".") + 1).ToLower();
archivo = "Imagen_" + numero + '_' + Pais + extension;
file.SaveAs(ruta + archivo);
string urlImgen = "/Home/Imagenes_Landing/" + archivo;
ViewBag.Message = ManejoDatos.AgregarRegistro_LandingPage("Amairani Fernanda Rodriguez", Pais, fecAct, fecVen, tituloLanzamiento,
urlImgen, urlPDF);
}
return RedirectToAction("NuevosLanzamientos", "Home");
}
In the method called Insert_Data, a list was created for the images, and traversing it with the foreach.