Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

.net core web api save pdf file in local folder

public class HomeController : Controller
{
    private readonly IHostingEnvironment _hostingEnv;
    private readonly ApplicationDbContext _context;

    public HomeController(IHostingEnvironment hostingEnv,ApplicationDbContext context)
    {
        _hostingEnv = hostingEnv;
        _context = context;
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    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();
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# how to refresh input field 
Csharp :: jobject alternative in system.text.json 
Csharp :: vb.net read registry key as string 
Csharp :: dataannotations for currency in c# 
Csharp :: Unity Input Key Message 
Csharp :: atan2 speed unity 
Csharp :: iserviceprovider vs iservicecollection 
Csharp :: selecteditem treeview wpf 
Csharp :: discord bot c# how to refresh message 
Csharp :: c# yes no cancel dialog with icons 
Csharp :: c# ping all machines on local network 
Csharp :: C# multiple button click event to textbox 
Csharp :: asp.net core 6 get current culture in controller 
Csharp :: messagebox error c# 
Csharp :: visual studio 2019 problem create new .net framework class library 
Csharp :: executesqlinterpolatedasync stored procedure 
Csharp :: pause and resume thread C# 
Csharp :: asp.net list size 
Csharp :: make first 2 words upper case c# 
Csharp :: unity mouse click 
Csharp :: wpf loop through grid rows 
Csharp :: serenity get id from insert repository 
Csharp :: vb.net get double item in list osf string 
Csharp :: c# function<T 
Csharp :: c# statements 
Csharp :: generate random light color android 
Csharp :: how to remove all controls from panel c# 
Csharp :: "; expected" error c#$ 
Csharp :: get innermost exception c# 
Csharp :: c# azure get vm get cpu usage 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =