asp.net core upload file as part of a formASP.NET Web Site or ASP.NET Web Application?Date only from TextBoxFor()File Upload ASP.NET MVC 3.0Validation failed for one or more entities while saving changes to SQL Server Database using Entity FrameworkReturning a file to View/Download in ASP.NET MVCDeleting an uploaded file from the server in ASP.NET MVC3HTTP Error 503, the service is unavailableUpload multiple files/ filepath using asp.net mvcHow to use npm with ASP.NET Coreuploading image asp.net Core

Does IPv6 have similar concept of network mask?

Can disgust be a key component of horror?

Does the UK parliament need to pass secondary legislation to accept the Article 50 extension

How do you make your own symbol when Detexify fails?

Make a Bowl of Alphabet Soup

Strong empirical falsification of quantum mechanics based on vacuum energy density?

Bridge building with irregular planks

Add big quotation marks inside my colorbox

Did arcade monitors have same pixel aspect ratio as TV sets?

Has any country ever had 2 former presidents in jail simultaneously?

Why is so much work done on numerical verification of the Riemann Hypothesis?

Does malloc reserve more space while allocating memory?

Quoting Keynes in a lecture

A binary search solution to 3Sum

When were female captains banned from Starfleet?

Why is short-wave infrared portion of electromagnetic spectrum so sensitive to fire?

Can I still be respawned if I die by falling off the map?

The IT department bottlenecks progress, how should I handle this?

Recommended PCB layout understanding - ADM2572 datasheet

Pre-mixing cryogenic fuels and using only one fuel tank

What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?

How to create table with 2D function values?

Multiplicative persistence

Unexpected behavior of the procedure `Area` on the object 'Polygon'



asp.net core upload file as part of a form


ASP.NET Web Site or ASP.NET Web Application?Date only from TextBoxFor()File Upload ASP.NET MVC 3.0Validation failed for one or more entities while saving changes to SQL Server Database using Entity FrameworkReturning a file to View/Download in ASP.NET MVCDeleting an uploaded file from the server in ASP.NET MVC3HTTP Error 503, the service is unavailableUpload multiple files/ filepath using asp.net mvcHow to use npm with ASP.NET Coreuploading image asp.net Core













0















Trying to upload a file as part of a form and store it in a folder in wwwroot then store the url for the file in a sql server database table along with the other details of the form? If anyone has any ideas it would be greatly appriciated










share|improve this question







New contributor




Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • can you share some code from your controllers/backend?

    – Aarif
    yesterday











  • is it a REST endpoint or server rendered page?

    – Aarif
    yesterday






  • 1





    Hey Karl, this request is a little broad in scope for StackOverflow. Have a google for file upload examples, and if you struggle with that, come back here with more specific questions. The link below should start you off nicely with this task, good luck with it! docs.microsoft.com/en-us/aspnet/core/mvc/models/…

    – Ted
    yesterday















0















Trying to upload a file as part of a form and store it in a folder in wwwroot then store the url for the file in a sql server database table along with the other details of the form? If anyone has any ideas it would be greatly appriciated










share|improve this question







New contributor




Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • can you share some code from your controllers/backend?

    – Aarif
    yesterday











  • is it a REST endpoint or server rendered page?

    – Aarif
    yesterday






  • 1





    Hey Karl, this request is a little broad in scope for StackOverflow. Have a google for file upload examples, and if you struggle with that, come back here with more specific questions. The link below should start you off nicely with this task, good luck with it! docs.microsoft.com/en-us/aspnet/core/mvc/models/…

    – Ted
    yesterday













0












0








0








Trying to upload a file as part of a form and store it in a folder in wwwroot then store the url for the file in a sql server database table along with the other details of the form? If anyone has any ideas it would be greatly appriciated










share|improve this question







New contributor




Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












Trying to upload a file as part of a form and store it in a folder in wwwroot then store the url for the file in a sql server database table along with the other details of the form? If anyone has any ideas it would be greatly appriciated







asp.net asp.net-mvc asp.net-core






share|improve this question







New contributor




Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Karl McgeoughKarl Mcgeough

1




1




New contributor




Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Karl Mcgeough is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • can you share some code from your controllers/backend?

    – Aarif
    yesterday











  • is it a REST endpoint or server rendered page?

    – Aarif
    yesterday






  • 1





    Hey Karl, this request is a little broad in scope for StackOverflow. Have a google for file upload examples, and if you struggle with that, come back here with more specific questions. The link below should start you off nicely with this task, good luck with it! docs.microsoft.com/en-us/aspnet/core/mvc/models/…

    – Ted
    yesterday

















  • can you share some code from your controllers/backend?

    – Aarif
    yesterday











  • is it a REST endpoint or server rendered page?

    – Aarif
    yesterday






  • 1





    Hey Karl, this request is a little broad in scope for StackOverflow. Have a google for file upload examples, and if you struggle with that, come back here with more specific questions. The link below should start you off nicely with this task, good luck with it! docs.microsoft.com/en-us/aspnet/core/mvc/models/…

    – Ted
    yesterday
















can you share some code from your controllers/backend?

– Aarif
yesterday





can you share some code from your controllers/backend?

– Aarif
yesterday













is it a REST endpoint or server rendered page?

– Aarif
yesterday





is it a REST endpoint or server rendered page?

– Aarif
yesterday




1




1





Hey Karl, this request is a little broad in scope for StackOverflow. Have a google for file upload examples, and if you struggle with that, come back here with more specific questions. The link below should start you off nicely with this task, good luck with it! docs.microsoft.com/en-us/aspnet/core/mvc/models/…

– Ted
yesterday





Hey Karl, this request is a little broad in scope for StackOverflow. Have a google for file upload examples, and if you struggle with that, come back here with more specific questions. The link below should start you off nicely with this task, good luck with it! docs.microsoft.com/en-us/aspnet/core/mvc/models/…

– Ted
yesterday












1 Answer
1






active

oldest

votes


















0














You could use IFormFile to receive the file.And then save the file path url to your database using EF core. Remember to create a myFiles folder where to save uploaded files under wwwroot firstly.



You could refer to the tutorial of File uploads in ASP.NET Core



Below is a simple demo:



Models:



public class Engineer

public int Id get; set;
public string Name get; set;
public string FilePath get; set;

public class EngineerVM

public string Name get; set;
public IFormFile File get; set;



View:



@model EngineerVM
<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
<div class="form-group">
<div class="col-md-10">
<input type="text" asp-for="Name" />
</div>
<div class="col-md-10">
<p>Upload one or more files using this form:</p>
<input type="file" name="file"/>
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input type="submit" value="Save" />
</div>
</div>
</form>


Controller:



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);
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();







share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Karl Mcgeough is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55280569%2fasp-net-core-upload-file-as-part-of-a-form%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could use IFormFile to receive the file.And then save the file path url to your database using EF core. Remember to create a myFiles folder where to save uploaded files under wwwroot firstly.



    You could refer to the tutorial of File uploads in ASP.NET Core



    Below is a simple demo:



    Models:



    public class Engineer

    public int Id get; set;
    public string Name get; set;
    public string FilePath get; set;

    public class EngineerVM

    public string Name get; set;
    public IFormFile File get; set;



    View:



    @model EngineerVM
    <form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
    <div class="form-group">
    <div class="col-md-10">
    <input type="text" asp-for="Name" />
    </div>
    <div class="col-md-10">
    <p>Upload one or more files using this form:</p>
    <input type="file" name="file"/>
    </div>
    </div>
    <div class="form-group">
    <div class="col-md-10">
    <input type="submit" value="Save" />
    </div>
    </div>
    </form>


    Controller:



    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);
    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();







    share|improve this answer



























      0














      You could use IFormFile to receive the file.And then save the file path url to your database using EF core. Remember to create a myFiles folder where to save uploaded files under wwwroot firstly.



      You could refer to the tutorial of File uploads in ASP.NET Core



      Below is a simple demo:



      Models:



      public class Engineer

      public int Id get; set;
      public string Name get; set;
      public string FilePath get; set;

      public class EngineerVM

      public string Name get; set;
      public IFormFile File get; set;



      View:



      @model EngineerVM
      <form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
      <div class="form-group">
      <div class="col-md-10">
      <input type="text" asp-for="Name" />
      </div>
      <div class="col-md-10">
      <p>Upload one or more files using this form:</p>
      <input type="file" name="file"/>
      </div>
      </div>
      <div class="form-group">
      <div class="col-md-10">
      <input type="submit" value="Save" />
      </div>
      </div>
      </form>


      Controller:



      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);
      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();







      share|improve this answer

























        0












        0








        0







        You could use IFormFile to receive the file.And then save the file path url to your database using EF core. Remember to create a myFiles folder where to save uploaded files under wwwroot firstly.



        You could refer to the tutorial of File uploads in ASP.NET Core



        Below is a simple demo:



        Models:



        public class Engineer

        public int Id get; set;
        public string Name get; set;
        public string FilePath get; set;

        public class EngineerVM

        public string Name get; set;
        public IFormFile File get; set;



        View:



        @model EngineerVM
        <form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
        <div class="form-group">
        <div class="col-md-10">
        <input type="text" asp-for="Name" />
        </div>
        <div class="col-md-10">
        <p>Upload one or more files using this form:</p>
        <input type="file" name="file"/>
        </div>
        </div>
        <div class="form-group">
        <div class="col-md-10">
        <input type="submit" value="Save" />
        </div>
        </div>
        </form>


        Controller:



        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);
        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();







        share|improve this answer













        You could use IFormFile to receive the file.And then save the file path url to your database using EF core. Remember to create a myFiles folder where to save uploaded files under wwwroot firstly.



        You could refer to the tutorial of File uploads in ASP.NET Core



        Below is a simple demo:



        Models:



        public class Engineer

        public int Id get; set;
        public string Name get; set;
        public string FilePath get; set;

        public class EngineerVM

        public string Name get; set;
        public IFormFile File get; set;



        View:



        @model EngineerVM
        <form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
        <div class="form-group">
        <div class="col-md-10">
        <input type="text" asp-for="Name" />
        </div>
        <div class="col-md-10">
        <p>Upload one or more files using this form:</p>
        <input type="file" name="file"/>
        </div>
        </div>
        <div class="form-group">
        <div class="col-md-10">
        <input type="submit" value="Save" />
        </div>
        </div>
        </form>


        Controller:



        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);
        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();








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 15 hours ago









        Xing ZouXing Zou

        847116




        847116






















            Karl Mcgeough is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Karl Mcgeough is a new contributor. Be nice, and check out our Code of Conduct.












            Karl Mcgeough is a new contributor. Be nice, and check out our Code of Conduct.











            Karl Mcgeough is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55280569%2fasp-net-core-upload-file-as-part-of-a-form%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해