Name of the files when saving in C # MVC (BodyPart)

3

I am programming an input file with angularJS and c # MVC. At the time of reviewing the folder where you keep the files shows them to me in the following way

So I have my code on the controller

    public class SubirHvController : ApiController
{
    [System.Web.Http.HttpPost]
    [System.Web.Http.Route("PostFileWithData")]
    public async Task<HttpResponseMessage> Post()
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        var root = HttpContext.Current.Server.MapPath("~/App_Data/Uploadfiles");
        Directory.CreateDirectory(root);
        var provider = new MultipartFormDataStreamProvider(root);
        var result = await Request.Content.ReadAsMultipartAsync(provider);


        var model = result.FormData["jsonData"];
        if (model == null)
        {
            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }
        //TODO: Do something with the JSON data.  

        //get the posted files  
        foreach (var file in result.FileData)
        {
            //TODO: Do something with uploaded file.  
        }

        return Request.CreateResponse(HttpStatusCode.OK, "success!");
    }
}

It seems that if the data is arriving well

Any opinion would be very helpful. Thanks

    
asked by Camilo López 22.03.2018 в 14:39
source

2 answers

3

Exactly. The server to avoid stepping on files and not having problems when saving, saves them in that way

If you want to know the real name of the file that the user sent, you have it in:

file.Headers.ContentDisposition.FileName

and the name it has on the server is:

file.LocalFileName

That way, if you add that information in the foreach that runs through what came, you will know the name of each file.

    
answered by 22.03.2018 в 18:25
0

If I kept the files well, I could not change the name or the extension. This was the solution I found in case someone serves it later.

Create another class and assign these lines of code to be able to modify the name of the files to my liking.

Main class

    public class SubirHvController : ApiController
{
    [System.Web.Http.HttpPost]
    [System.Web.Http.Route("SubirHojaDeVida")]
    public async Task<HttpResponseMessage> Post()
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        var ruta = HttpContext.Current.Server.MapPath("~/App_Data/Uploadfiles");
        Directory.CreateDirectory(ruta);


        var datos = new FormDataStreamer(ruta);

        datos.NombreArchivo = MetodosUtiles.ObtenerValorUnico() + "_SolicitudHojaDeVida";

        var result = await Request.Content.ReadAsMultipartAsync(datos);

        return Request.CreateResponse(HttpStatusCode.OK, "success!");
    }

}
    public class FormDataStreamer : MultipartFormDataStreamProvider
{
    public FormDataStreamer(string rootPath) : base(rootPath)
    {
    }

    public FormDataStreamer(string rootPath, int bufferSize) : base(rootPath, bufferSize)
    {
    }

    public string NombreArchivo { get; set; }

    public override string GetLocalFileName(HttpContentHeaders headers)
    {
        var srcFileName = headers.ContentDisposition.FileName.Replace("\"", "");
        return NombreArchivo + Path.GetExtension(srcFileName);
    }
}
    
answered by 23.03.2018 в 13:56