0

I hope someone can advise me on the situation I have. I have two asp mvc applications, one is the desktop and the other the mobile version. I want to be able to write files uploaded on the mobile version to a folder in the desktop version, because this latter holds the administrative section of the application. I can download files from one to the other with the WebClient class of .Net, but I haven't been able to upload files from one to the other. I have a console application to test the uploading and whenever I try it I get an error saying "access is denied" to the folder in the application. I tried creating a folder in the application outside of the App_Data folder because I thought this folder is restricted, but I get the same error. Can someone please advise me what to do? I've read googling here and there that this can be easily solved from the IIS administration, but I doubt the guys in my hosting company will be willing to grant me any rights on the administration of any application. The code I wrote only fails when I try to save the files. Here is the code of the site that receives the request:

[HttpPost]
    [AllowAnonymous]
    public string SubirArchivosRegistro(HttpPostedFileBase file)
    {
        string MensajeError = "";
        string path = "";
        string Success = " El (los) diplomas subidos exitosamente ";
        try
        {

                if (file != null && file.ContentLength > 0)
                {
                    path = Path.Combine(Server.MapPath("~/ArchivosRegistroUsuarios"), file.FileName);

                    file.SaveAs(path);
                    Success += file.FileName +",";
                }
                ///return path;
            return Success;
        }
        catch(Exception ex)
        {
            MensajeError = "Ha ocurrido un error, no se pudieron subir los archivos solicitados "+ex.Message;
            return MensajeError;
        }
    }

And this is the code in the client part (none of the codes is failing, it's just a permissions issue):

try
        {
            WebClient cliente = new WebClient();
            if (GendarmeriaCertNombre != null && GendarmeriaCertNombre.ElementAt(0) != null)
            {
                gendarmeria = new List<Guid?>();
                gendarmeriaNombre = new List<string>();
                foreach (HttpPostedFileBase file in GendarmeriaCertNombre)
                {
                    if (file != null)
                    {
                        string filename = Path.GetFileName(file.FileName);
                        Guid guidnum = _fileStore.SaveUploadedFile(file, "gendarmeria");
                        gendarmeria.Add(guidnum);
                        gendarmeriaNombre.Add(file.FileName);
                        string ruta = _fileStore.GetDiskLocation(guidnum);
                        byte[] respuesta = cliente.UploadFile("http://localhost/DiplomadoEnMandoPolicial/Account/SubirArchivosRegistro", "POST", ruta);
                        response = System.Text.Encoding.ASCII.GetString(respuesta);
                    }

                }
                if (gendarmeria != null && gendarmeria.Count > 0 && gendarmeriaNombre != null && gendarmeriaNombre.Count > 0)
                {
                    registro.GendarmeriaCertificado = String.Join(",", gendarmeria.Select(t => t.Value.ToString("D")));
                    registro.GendarmeriaCertNombre = String.Join(",", gendarmeriaNombre);
                }


            }
            if (CursoInacipeCertNombre != null && CursoInacipeCertNombre.ElementAt(0) != null)
            {
                inacipe = new List<Guid?>();
                inacipeNombre = new List<string>();
                foreach (HttpPostedFileBase file in CursoInacipeCertNombre)
                {
                    if (file != null)
                    {
                        string filename = Path.GetFileName(file.FileName);
                        Guid guidnum = _fileStore.SaveUploadedFile(file, "inacipe");
                        inacipe.Add(guidnum);
                        inacipeNombre.Add(file.FileName);
                        string ruta = _fileStore.GetDiskLocation(guidnum);
                        byte[] respuesta = cliente.UploadFile("http://localhost/DiplomadoEnMandoPolicial/Account/SubirArchivosRegistro", "POST", ruta);
                        response = System.Text.Encoding.ASCII.GetString(respuesta);
                    }
                }
                if (inacipe != null && inacipe.Count > 0 && inacipeNombre != null && inacipeNombre.Count > 0)
                {
                    registro.CursoInacipeCertificado = String.Join(",", inacipe.Select(t => t.Value.ToString("D")));
                    registro.CursoInacipeCertNombre = String.Join(",", inacipeNombre);
                }

            }
            if (CursoCideCertNombre != null && CursoCideCertNombre.ElementAt(0) != null)
            {
                cide = new List<Guid?>();
                cideNombre = new List<string>();
                foreach (HttpPostedFileBase file in CursoCideCertNombre)
                {
                    if (file != null)
                    {
                        string filename = Path.GetFileName(file.FileName);
                        Guid guidnum = _fileStore.SaveUploadedFile(file, "cide");
                        cide.Add(guidnum);
                        cideNombre.Add(file.FileName);
                        string ruta = _fileStore.GetDiskLocation(guidnum);
                        byte[] respuesta = cliente.UploadFile("http://localhost/DiplomadoEnMandoPolicial/Account/SubirArchivosRegistro", "POST", ruta);
                        response = System.Text.Encoding.ASCII.GetString(respuesta);
                    }
                }
                if (cide != null && cide.Count > 0 && cideNombre != null && cideNombre.Count > 0)
                {
                    registro.CursoCideCertificado = String.Join(",", cide.Select(t => t.Value.ToString("D")));
                    registro.CursoCideCertNombre = String.Join(",", cideNombre);
                }
                cliente.Dispose();
            }
        }

Well, that's the problem, I'm open to any suggestions.

gerardo flores
  • 402
  • 1
  • 6
  • 28
  • Test 1: check if you can upload to same application (first see if you can do this)? It is most likely the two applications are running under two different application pool identities. Usually hosting companies would have it this way so that each web application could be managed separately without interfering with the other. If Test1 is fine you may have to give the appropriate permissions to the app pool identity of app 1 to write files to app 2. Have a look at this SO thread http://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permissions – Prashanth Thurairatnam Sep 13 '15 at 22:35
  • @Prashanth Thurairatnam Sorry I didin't quite got you. Test 1 would be to test if I can upload a file from the first application (I wouldn't use WebClient then). I've checked this and it's ok. Otherwise I guess it's a problem with IIS as you suggest. Thank you for your comment. I'll let you know how it goes. – gerardo flores Sep 14 '15 at 00:02

0 Answers0