Duplicate files when uploading them in ASP.NET MVC

4

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

    
asked by Amairani Fernanda 13.06.2017 в 20:00
source

2 answers

4

In case anyone ever needs it, it was resolved as follows:

In the controller I created a method to register the PDF and it was sent to call 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 traversed with the foreach.

    
answered by 15.06.2017 / 15:19
source
1

If in the action you define two parameters HttpPostedFileBase urlImg, HttpPostedFileBase urlPdf , you should not use the Request.Files because if in the post you define:

<input type="file" name="urlImg" />
<input type="file" name="urlPdf" />

you could take the files directly in the parameters of the action, you will see that the name matches the name of the action parameter, in this way you will have the two files separated without having to iterate in a loop.

You also save twice from the same variable, but you loop with the% for% co at the end you end up using Request.Files on the line

urlPdf.SaveAs(rutaPDF + documento);

where this variable is the pdf you upload when submitting, you should do the save using urlPdf

file.SaveAs(rutaPDF + documento);

which is the variable you define in file

    
answered by 13.06.2017 в 21:34