Exemplo n.º 1
0
        public async Task <IActionResult> UploadFile(IFormFile file)
        {
            try
            {
                //string foldpath = _helperService.RandomString(16, true);
                //string folderName = "..\\..\\Apps\\fmr\\Media\\"+ foldpath;
                //string dr = Directory.GetCurrentDirectory();
                //string newPath = Path.Combine(dr, folderName);
                //if (!Directory.Exists(newPath))
                //{
                //    Directory.CreateDirectory(newPath);
                //}

                string fileName = "";
                fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                FileInfo fi = new FileInfo(fileName);
                if (fi.Extension.ToLower() == ".webm")
                {
                    string fileN    = _helperService.RandomString(8, true) + fi.Extension;
                    string folder   = _helperService.GetMediaPath() + "\\temp";
                    string dr       = Directory.GetCurrentDirectory();
                    string savePath = Path.Combine(Directory.GetParent(dr).Parent.ToString(), folder);
                    if (!Directory.Exists(savePath))
                    {
                        Directory.CreateDirectory(savePath);
                    }
                    string fullPath = Path.Combine(savePath, fileN);
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }
                    return(Ok(new { StatusCode = StatusCodes.Status200OK, message = "File Uploded Successfully", result = fullPath }));
                }

                return(Ok(new { StatusCode = StatusCodes.Status200OK, message = "invalid file format" }));
            }
            catch (System.Exception ex)
            {
                return(Ok(new { StatusCode = StatusCodes.Status417ExpectationFailed, message = ex.Message }));
            }
        }