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

4

I would like to get the 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" })

Thank you for all the answers.

    
asked by anonymous 26.10.2015 / 19:46

1 answer

3
Assuming 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 of type HttpPostedFileBase .

You can also manipulate the bytes directly through the InputStream ".

The file does not exactly have a path . The file is usually in the request body, through a sequence of bytes.

    
26.10.2015 / 19:50