How to get the path of a file (Httppostedfilebase)?

Asked

Viewed 851 times

4

I would like to get the path(path) from where my file is coming from.

Model:

    [Required(ErrorMessage = "Selecione o arquivo a ser importado.")]
    [Display(Name = "Arquivo ")]
    public HttpPostedFileBase Arquivo { get; set; }

View:

@Html.LabelFor(a => a.Arquivo)
@Html.TextBoxFor(a => a.Arquivo, new { type = "file", name="Arquivo" })

I appreciate all the answers.

1 answer

3

Assuming a Action of Controller, you can get the file as follows:

    private const String DiretorioCurriculos = "~/Curriculos/";

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> EnviarCurriculo([Bind(Include = "CurriculoCandidatoId,NomeCandidato,Email,TelefoneContato,TelefoneCelular,ArquivoCurriculo,SetorPreterido,Comentarios")] CurriculoCandidato curriculoCandidato)
    {
        if (curriculoCandidato.Arquivo != null && curriculoCandidato.Arquivo.ContentLength > 0)
        {
            string nomeArquivo = Regex.Replace(curriculoCandidato.NomeCandidato + "-" + Path.GetFileName(curriculoCandidato.Arquivo.FileName), @"\s+", "");
            string caminho = Path.Combine(Server.MapPath(DiretorioCurriculos), nomeArquivo);
            curriculoCandidato.Arquivo.SaveAs(caminho);

            curriculoCandidato.CaminhoArquivoCurriculo = nomeArquivo;
        }

        ...

Arquivo is the type HttpPostedFileBase.

You can also manipulate bytes directly through the property InputStream.

The file doesn’t exactly have one path. The file is usually in the body of the request, through a sequence of bytes.

  • Thanks to the @Gypsy reply, however, I really need the file path... by my model I can already access the file Inputstream and manipulate what I need it, but for another logic I really need the physical location where the file came from at the time of Submit.

  • You, the customer machine site?

  • in case, the file will always come from a server, however it may come from separate folders.

  • HttpPostedFileBase does not save information from the file’s source directory. I think you will have to rethink its logic.

Browser other questions tagged

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