How to upload a file to the server - Asp . net MVC

Asked

Viewed 248 times

1

Good afternoon, I did a small project on Asp . net whose only goal is to select a file on my computer and save it on the server. The problem is that I am unable to perform this procedure after publishing the project on IIS. The code I’m using is as follows::

Controller:

 public ActionResult SalvarArquivo(HttpPostedFileBase file)
    {
        if(file == null)
        {
            return RedirectToAction("Index", "Upload"); ;
        }

        try
        {
            if(file.ContentLength > 0)
            {
                var nomeArquivo = Path.GetFileName(file.FileName);

                string[] split = nomeArquivo.Split('.');

                if(split[1] == "xlsx")
                {
                    //var caminho = Path.Combine(Server.MapPath("~/Uploads"), "teste.xlsx");

                    string path = string.Format(@"c:\inetpub\ftproot\minhapasta\{0}", "teste.xlsx");

                    file.SaveAs(path);

                    ViewBag.Mensagem = "Arquivo enviado e salvo com sucesso no servidor.";

                    return RedirectToAction("Index", "Home");

                }
                else
                {
                    ViewBag.Mensagem = "O Arquivo enviado não é válido, selecione um arquivo com extensão 'xlsx'!";

                    return RedirectToAction("Index", "Home");
                }

            }
        }
        catch (Exception e)
        {
            ViewBag.Mensagem = string.Format("Não foi possível enviar o arquivo: {0}", e.Message);

            return RedirectToAction("Index", "Home");

        }

        return RedirectToAction("Index", "Home");

    }

View:

<h2>SalvarArquivo</h2>


<h1>AlliBus - Carga de Planilha </h1>
<hr />
<div>
<form action="/Upload/SalvarArquivo" method="post" enctype="multipart/form- 
 data">
    <label for="file">Nome do Arquivo :</label>
    <br />
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" />
    <p>
        @ViewBag.Mensagem
</form>
</div>

Inside the server, the path where I want to save the file is in: "c: Inetpub ftproot minhapasta ". How can I save my file on this site? Thanks in advance to anyone who can help. Regards.

  • 1

    Why??? How is configured your application is within ftproot??? Does this folder have authorization to record files??? I am not getting to understand how is your application and why of this absolute path to record this file, could you explain?? What or what mistakes happen or problems?

  • Hello Virgilio, thank you for answering. Well, my application is on the way "C: Inetpub wwwroot minhaaplicacao" and I want to record the file in "c: Inetpub ftproot minhapasta". As far as I can tell, I’m authorized to make recordings in this folder. Regarding errors, it simply does not appear any, when I run the application locally, it saves the file in the specified path, however when I do the post and ask to save in the informed folder it does not save.

  • strange not to appear error, because it has to have some information is a problem that without checking with the server and service becomes complicated to say anything.

  • @Virgilionovic, I get it, but at least in theory the code is right? in the sense of accessing another folder inside the server? I’ll run a few more tests, to see if I can get any error message. Thanks again.

  • depends on the permission and where the Server can reach, it is not normal what you are doing ...

  • Manually edit the file Web.Config published server, adding the property debug="true" on the tag compilation. This will allow showing the error while trying to save the file again. Try ae and see the error...

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.