we want to upload just one kind of file so inside the controller we have put, this code:
private async Task UploadImageAsync(string id, string kind,
IList uploadFiles)
{
// TODO: thread safety.
// save in local cache the files uploaded.
if (uploadFiles.Count != 1)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
try
{
// just sure of thread safety here.
var file = uploadFiles.ElementAt(0);
if (!IsValidFormat(file.ContentType))
{
var response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.NoContent;
response.ReasonPhrase = "Unsupported file format";
return response;
}
var filename = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
// mangle the file name here.
var photo = new PhotoDto();
var cachedPhoto = new PhotoDto();
photo.VehicleCode = id;
photo.Kind = kind;
using (var ms = new MemoryStream())
{
await file.CopyToAsync(ms);
photo.Data = ms.ToArray();
}
photo.Name = filename;
photo.Hash = HashFile(photo.Data);
photo.UpdateCode();
// here we save it in the cache.
if (_cache.TryGetValue(photo.Code, out cachedPhoto))
{
_cache.Remove(cachedPhoto.Code);
_cache.Set(cachedPhoto.Code, photo);
}
else
{
_cache.Set(photo.Code, photo);
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (Exception e)
{
var httpResponse = new HttpResponseMessage(HttpStatusCode.NoContent);
httpResponse.ReasonPhrase = "File failed to upload : " + e.Message;
return httpResponse;
}
}
}
}
But ui says always is successful uploaded in ejs-upload. Any hints?