I have the following code which I use to register images and pdf's, the problem is that up to the moment when I register for example two different images I save 1 but 2 times. And in the case of the PDF what I want is for me to save only one for the images I upload. When I register them, I keep the URL of both the images and the pdf.
The <input>
I use for the 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, all of them are different, not the same one 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