コード例 #1
0
        public async Task <IHttpActionResult> UploadSummary(int id)
        {
            try
            {
                if (!Request.Content.IsMimeMultipartContent())
                {
                    return(BadRequest());
                }
                var provider = new MultipartMemoryStreamProvider();
                var root     = HttpContext.Current.Server.MapPath("~/Content/Candidates/" + id + "/Summary/");
                await Request.Content.ReadAsMultipartAsync(provider);

                if (!Directory.Exists(root))
                {
                    Directory.CreateDirectory(root);
                }

                var dirInfo = new DirectoryInfo(root);
                foreach (var f in dirInfo.GetFiles())
                {
                    f.Delete();
                }

                var    file      = provider.Contents[0];
                var    filename  = file.Headers.ContentDisposition.FileName.Trim('\"');
                byte[] fileArray = await file.ReadAsByteArrayAsync();

                using (FileStream fs = new FileStream(root + filename, FileMode.Create))
                {
                    await fs.WriteAsync(fileArray, 0, fileArray.Length);
                }
                var filePath = "/Content/Candidates/" + id + "/Summary/" + filename;
                await repository.EditCandidateSummaryPath(id, filePath);

                return(Ok(filePath));
            }
            catch (Exception)
            {
                return(BadRequest("Произошла ошибка при загрузке HR-конспекта"));
            }
        }