示例#1
0
        public async Task <HttpResponseMessage> Post()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, "please submit a valid request"));
            }
            var provider = new MultipartMemoryStreamProvider();

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                var file = provider.Contents.FirstOrDefault(x => !string.IsNullOrEmpty(x.Headers.ContentDisposition.FileName));
                if (file != null)
                {
                    InitHttpClient();
                    var stream   = file.ReadAsStreamAsync().Result;
                    var filename = SaveFile(stream);
                    var products = _excelReader.GetProductsFromFile(filename);
                    var productsWithChangedProperties = FindProductsWithChangedProperties(products);

                    return(Request.CreateResponse(HttpStatusCode.OK, productsWithChangedProperties, "application/json"));
                }

                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Неверный файл"));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }