示例#1
0
        public IActionResult Upload([FromForm] IFormFile file1)
        {
            if (file1 == null)
            {
                return(BadRequest("must upload a file"));
            }
            //check file extension

            try
            {
                //var file = Request.Form.Files[0];
                var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), "UploadFiles");
                if (file1.Length > 0)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file1.ContentDisposition).FileName.Trim('"');
                    var fullPath = Path.Combine(pathToSave, fileName);
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file1.CopyTo(stream);
                    }
                    repository.UploadExcel(fullPath);
                    return(Ok("Upload successful"));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, $"Internal server error: {ex}"));
            }
        }