예제 #1
0
        public async Task<IHttpActionResult> Post(int tp)
        {
            var t = _tps.GetTPbyId(tp);
            if (t != null)
            {
                if (Request.Content.IsMimeMultipartContent())
                {
                    string uploadPath = HttpContext.Current.Server.MapPath(startPath + Guid.NewGuid().ToString());
                    string savePath = HttpContext.Current.Server.MapPath(startPath + t.folder.Trim());

                    MyStreamFileProvider streamProvider = new MyStreamFileProvider(uploadPath);

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

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

                    await Request.Content.ReadAsMultipartAsync(streamProvider);

                    List<string> messages = new List<string>();
                    foreach (var file in streamProvider.FileData)
                    {
                        FileInfo fi = new FileInfo(file.LocalFileName);
                        try
                        {
                            File.Copy(fi.FullName, savePath + "/" + fi.Name);
                            var nd = new Document()
                            {
                                filename = fi.Name,
                                description = fi.Name,
                                tpid = t.Id,
                                Id = -1
                            };
                            _drs.AddDocument(nd);
                            messages.Add("Загружен файл " + fi.Name + " (" + fi.Length + " байт)");
                        }
                        catch
                        {
                            messages.Add("Не загружен файл " + fi.Name + " (" + fi.Length + " байт)");
                        }
                    }
                    Request.Dispose();
                    Directory.Delete(uploadPath, true);
                    return Ok(messages);
                }
                else
                {
                    return BadRequest();
                }
            }
            else
            {
                return BadRequest("ТП не найдена");
            }
        }
예제 #2
0
        public async Task<IHttpActionResult> ImportNonOurTp()
        {
            if (Request.Content.IsMimeMultipartContent())
            {
                string uploadPath = HttpContext.Current.Server.MapPath(startPath + "tmp");

                MyStreamFileProvider streamProvider = new MyStreamFileProvider(uploadPath);

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

                await Request.Content.ReadAsMultipartAsync(streamProvider);

                List<string> messages = new List<string>();
                foreach (var file in streamProvider.FileData)
                {
                    FileInfo fi = new FileInfo(file.LocalFileName);
                    try
                    {
                        _tps.LoadNonOurFromSqlite(fi.FullName);
                        messages.Add("Загружен файл " + fi.Name + " (" + fi.Length + " байт)");
                    }
                    catch
                    {
                        messages.Add("Не загружен файл " + fi.Name + " (" + fi.Length + " байт)");
                        return BadRequest("Не загружены данные из файла с базой ТП.");
                    }
                }
                Request.Dispose();
                Directory.Delete(uploadPath, true);
                messages.Add("Не наши ТП импортированы.");
                return Ok(messages);
            }
            else
            {
                return BadRequest();
            }
        }