3
I have a WEB API
who receives a file via POST
follows the code of Controller
:
public async Task<HttpResponseMessage> Post()
{
// Ver se POST é MultiPart?
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
// Preparar CustomMultipartFormDataStreamProvider para carga de dados
// (veja mais abaixo)
string fileSaveLocation = HttpContext.Current.Server.MapPath("~/Arquivos/Uploads");
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);
List<string> files = new List<string>();
try
{
// Ler conteúdo da requisição para CustomMultipartFormDataStreamProvider.
await Request.Content.ReadAsMultipartAsync(provider);
foreach (MultipartFileData file in provider.FileData)
{
files.Add(Path.GetFileName(file.LocalFileName));
}
// OK se tudo deu certo.
return Request.CreateResponse(HttpStatusCode.OK, files);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
My method: CustomMultipartFormDataStreamProvider
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public CustomMultipartFormDataStreamProvider(string path)
: base(path) { }
public override string GetLocalFileName(HttpContentHeaders headers)
{
return headers.ContentDisposition.FileName;
}
}
I’m saving the files in a folder Files/Uploads however, I need to save the files that are received in separate folders, for example Word in a folder called Word etc....
The way I’m doing it now is a little bureaucratic but it works. I have a class Utilities that upon receiving the file it makes a copy of the file to the folder it should be in and deletes the file inside the folder Files/Uploads.
There is a way to simplify this, which through the controller Post
i identify the file extension and save it to the respective folder on the server?
Is the folder name exactly equal to the extension? The folder is already created or should be created if it does not exist?
– Randrade
Until then I created some folders with the most known extensions, and had thought to leave the ones that are not so well known in a folder "Others". However it is interesting this approach, to create the folder if it does not exist.
– Renan Carlos
Renan, you forgot the first question. The folder name will be the same as the extension?
– Randrade
True, yes the same name.
– Renan Carlos
That one
CustomMultipartFormDataStreamProvider
is something you have created or some external lib?– Randrade
I implicit it, edited the question and inserted it. To save the file name. I saw in a reply here in the PT-SO http://answall.com/a/153255/61561
– Renan Carlos
How’s your utility class code?
– Leonel Sanches da Silva