示例#1
0
    public async Task <IActionResult> Index(EngineerVM engineerVM)
    {
        if (engineerVM.File != null)
        {
            //upload files to wwwroot
            var fileName = Path.GetFileName(engineerVM.File.FileName);
            //judge if it is pdf file
            string ext = Path.GetExtension(engineerVM.File.FileName);
            if (ext.ToLower() != ".pdf")
            {
                return(View());
            }
            var filePath = Path.Combine(_hostingEnv.WebRootPath, "images", fileName);
            using (var fileSteam = new FileStream(filePath, FileMode.Create))
            {
                await engineerVM.File.CopyToAsync(fileSteam);
            }
            //your logic to save filePath to database, for example

            Engineer engineer = new Engineer();
            engineer.Name     = engineerVM.Name;
            engineer.FilePath = filePath;

            _context.Engineers.Add(engineer);
            await _context.SaveChangesAsync();
        }
        else
        {
        }
        return(View());
    }
示例#2
0
        public async Task <IActionResult> Index(EngineerVM engineerVM)
        {
            string filepath = FilePath(engineerVM);

            Engineer engineer = new Engineer();

            engineer.Name     = engineerVM.Name;
            engineer.FilePath = filepath;

            _context.Engineers.Add(engineer);
            await _context.SaveChangesAsync();

            //}
            //else
            //{

            //}
            return(View());
        }
示例#3
0
        public string FilePath(EngineerVM engineerVM)
        {
            var filePath = "";

            if (engineerVM.File != null)
            {
                //upload files to wwwroot
                var fileName = Path.GetFileName(engineerVM.File.FileName);
                //judge if it is pdf file
                string ext = Path.GetExtension(engineerVM.File.FileName);
                if (ext.ToLower() != ".pdf")
                {
                    return("");
                }
                filePath = Path.Combine(_hostingEnv.WebRootPath, "myFiles", fileName);

                using (var fileSteam = new FileStream(filePath, FileMode.Create))
                {
                    engineerVM.File.CopyToAsync(fileSteam);
                }
            }
            return(filePath);
        }