Get file sent by POST

1

How do I get the file that is received here:

    [HttpPost]
    public async Task<IHttpActionResult> Post(HttpRequestMessage request)

I am attaching the file like this, but it is sent without extension and with a name I do not want

                foreach (MultipartFileData file in provider.FileData)
                {
              
                    mail.Attachments.Add(new Attachment(file.LocalFileName));
                }
    
asked by Carlos 16.05.2018 в 03:07
source

2 answers

0

It was easier than I thought.

I am resolved as follows.

                foreach (MultipartFileData file in provider.FileData)
                {
                    file.Headers.ContentDisposition.Name = "Nombre del Documento";                  
                    Attachment data = new Attachment(file.LocalFileName, System.Net.Mime.MediaTypeNames.Application.Pdf); //Attachment tiene varias sobrecargas
                    data.Name = subject;
                    mail.Attachments.Add(data);
                }
    
answered by 16.05.2018 / 14:11
source
0

Follow this example, it will help you, look in the foreach fileHeaders

[HttpPost, Route("api/upload")]
public async Task<IHttpActionResult> Upload()
{
    if (!Request.Content.IsMimeMultipartContent())
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 

    var provider = new MultipartMemoryStreamProvider();
    await Request.Content.ReadAsMultipartAsync(provider);
    foreach (var file in provider.Contents)
    {
        var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
        var buffer = await file.ReadAsByteArrayAsync();
        //Do whatever you want with filename and its binaray data.
    }

    return Ok();
}
    
answered by 16.05.2018 в 09:45