Esempio n. 1
0
        public async Task <IHttpActionResult> UploadInterview(int id)
        {
            try
            {
                if (!Request.Content.IsMimeMultipartContent())
                {
                    return(BadRequest());
                }
                var provider = new MultipartMemoryStreamProvider();
                var root     = HttpContext.Current.Server.MapPath("~/Content/Candidates/" + id + "/Interview/");
                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 + "/Interview/" + filename;
                await repository.EditCandidateInterviewPath(id, filePath);

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