Automatic Upload After File Upload Change Vb.net Asp
Implementation Summary
- Create a "tblMembers" tabular array in your Database.
- Create a new ASP.Net MVC web application project called "ClubMember".
- Create a Model called "MemberModel".
- Create HTTPGET ActionMethod called "ContactForm".
- Create a view of "ContactForm".
- Use HTML File Upload control.
- Create HTTPPOST ActionMethod chosen "ContactForm" to insert record in tabular array and salve file in Server.
- Add Linq To Sql course "ClubMember".
- Insert a record into a database table.
Footstep past step Implementation
In the above course, you can see there are four objects.
- Name Textbox
- Phone Number Textbox
- Image File Upload
- Submit Push button
Create a "tblMembers" tabular array in the database.
- Utilize [MbkTest]
- Become
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- Set ANSI_PADDING ON
- Go
- CREATE TABLE [dbo].[tblMembers](
- [MemberID] [int ] IDENTITY(1,one) Non NULL ,
- [MemberName] [varchar ](50) Nada ,
- [PhoneNumber] [varchar ](50) NULL ,
- [ImagePath] [varchar ](500) NULL ,
- Main Cardinal Clustered
- (
- [MemberID]ASC
- )WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ Primary ]
- )ON [ PRIMARY ]
- GO
- Prepare ANSI_PADDING OFF
- Go
Create a new ASP.Cyberspace MVC project chosen "ClubMember".
Click on "Alter Authentication".
But we are looking for the following output.
Click on "Change Authentication" push button and select No Authentication.
We have created a projection called "ClubMember". At present, we are going to add "MemberModel".
Correct-click on "MODELS" folder or press CTRL+SHIFT+A to add new Model (CLASS).
Requite Model Name: "MemberModel".
Code of MemberModel.cs
- using System;
- using Organisation.Collections.Generic;
- using System.Linq;
- using Organization.Spider web;
- namespace ClubMember.Models
- {
- public course MemberModel
- {
- public string Name { get; set; }
- public string PhoneNumber { get; prepare; }
- public string ImagePath { go; set; }
- public HttpPostedFile ImageFile { go; fix; }
- }
- }
Now, build your project by right-clicking on the project and selecting BUILD.
Now, switch to HOME Controller. Click on Controllers folder and double click on HOMECONTROLLER.CS file.
Create an action-method called CONTACTFORM.
By default, the Add View dialog box volition display every bit below.
Fill the "Add together VIEW" dialog box with the following values.
Every bit you click on Add push button in VIEWS-->HOME binder CONTACTFORM.CSHTML file will be created.
Switch to CONTACTFORM.CSHTML file and press F5. The output screen is given below.
Now, let us modify the CSHTML lawmaking.
Switch to CONTACTFORM.CSHTML file, exercise the post-obit changes and printing F5.
Remove following lawmaking of ImagePath
- @Html.EditorFor(model => model.ImagePath, new { htmlAttributes = new { @ form = "form-control" } })
- @Html.ValidationMessageFor(model => model.ImagePath,"" , new { @ class = "text-danger" })
Add the following lines of lawmaking.
- <input type= "file" name= "ImageFile" required />
The above lawmaking line is the HTML control for file uploading.
As we have changed and usedthe HTML File-Upload control named "ImageFile", and so now, permit u.s. change the model again to change the titles of fields similar this:
- Name = Member Name
- PhoneNumber = Telephone / Mobile Number
- ImagePath = Upload File
Code of MemberModel.cs
- using System;
- using System.Collections.Generic;
- using Organisation.ComponentModel;
- using Organisation.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- namespace ClubMember.Models
- {
- public class MemberModel
- {
- [DisplayName("Member Name" )]
- public cord Proper name { get; prepare; }
- [DisplayName("Phone / Mobile Number" )]
- public string PhoneNumber { get; set; }
- [DisplayName("Upload File" )]
- public string ImagePath { get; set; }
- public HttpPostedFileBase ImageFile { get; set; }
- }
- }
Note
DisplayName attribute comes after using the post-obit namespaces.
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
Now, again, switch to CONTACTFORM.CSHTML file, practise the following changes and press F5.
Now, permit us switch over again to ContactForm.cshtml to modify the following.
Line No. 10
@using (Html.BeginForm())
Change this to:
- @using(Html.BeginForm( "ContactForm" , "Home" , FormMethod.Mail service, new
- {
- enctype ="multipart/form-information"
- }))
Now, allow the states switch dorsum to the controller to piece of work on it.

- [HttpPost]
- public ActionResult ContactForm(MemberModel membervalues)
- {
- string FileName = Path.GetFileNameWithoutExtension(membervalues.ImageFile.FileName);
- string FileExtension = Path.GetExtension(membervalues.ImageFile.FileName);
- FileName = DateTime.Now.ToString("yyyyMMdd" )+ "-" +FileName.Trim()+ FileExtension;
- string UploadPath = ConfigurationManager.AppSettings["UserImagePath" ].ToString();
- membervalues.ImagePath = UploadPath + FileName;
- membervalues.ImageFile.SaveAs(membervalues.ImagePath);
- return View();
- }
After updating the HTTPPOST code, now, it is fourth dimension to create a new folder called "UserImages".
You can see in the Solution Explorer folder that the "UserImages" folder is created.
Now, open web.config file to add APPSETTING.
AppSetting exists under Configuration Tag.
- <configuration>
- <appSettings>
- <add key="webpages:Version" value= "3.0.0.0" />
- <add key="webpages:Enabled" value= "false" />
- <add key="ClientValidationEnabled" value= "truthful" />
- <add key="UnobtrusiveJavaScriptEnabled" value= "true" />
- <add fundamental="UserImagePath" value= "D:\MBK\ClubMember\ClubMember\UserImages\" />
- </appSettings>
In the higher up code, you tin can encounter UserImagePath key created with the value.
Now, fill the form.
After clicking on CREATE button and submitting the form, you tin can see your selected file copied in the fix binder in web.config.
At present, nosotros are going to write the code to shop other data of CONTACT FORM details.
- Fellow member Name
- Telephone/Mobile Number
- Image File Path
Right-click on project name "ClubMember".
Select Information--->LINQ to SQL Classes. Requite proper noun "ClubMemberDataClasses.dbml".
Open ClubMemberDataClasses.dbml file and open Server Explorer.
Click on the red button that is "Connect to database". Equally you click on this button, you lot will get the following dialog box.
In the higher up dialog box, y'all accept to provide a Database server name and Authentication level and afterward, select your database.
Now, you can see database is opened in Server Explorer.
Now, drag and drib tblMembers within ClubMemberDataClasses.dbml.
Now, switch back to HomeController to write INSERT record code into database table using LINQ TO SQL.
Code HomeController.cs
- using ClubMember.Models;
- using Arrangement;
- using System.Collections.Generic;
- using Arrangement.IO;
- using System.Linq;
- using System.Web;
- using Organisation.Web.Mvc;
- using Organisation.Configuration;
- namespace ClubMember.Controllers
- {
- public course HomeController : Controller
- {
- public ActionResult Alphabetize()
- {
- return View();
- }
- public ActionResult About()
- {
- ViewBag.Bulletin ="Your awarding description page." ;
- return View();
- }
- public ActionResult Contact()
- {
- ViewBag.Message ="Your contact page." ;
- return View();
- }
- [HttpGet]
- public ActionResult ContactForm()
- {
- return View();
- }
- [HttpPost]
- public ActionResult ContactForm(MemberModel membervalues)
- {
- string FileName = Path.GetFileNameWithoutExtension(membervalues.ImageFile.FileName);
- string FileExtension = Path.GetExtension(membervalues.ImageFile.FileName);
- FileName = DateTime.Now.ToString("yyyyMMdd" )+ "-" +FileName.Trim()+ FileExtension;
- string UploadPath = ConfigurationManager.AppSettings["UserImagePath" ].ToString();
- membervalues.ImagePath = UploadPath + FileName;
- membervalues.ImageFile.SaveAs(membervalues.ImagePath);
- var db = new ClubMemberDataClassesDataContext();
- tblMember _member =new tblMember();
- _member.ImagePath = membervalues.ImagePath;
- _member.MemberName = membervalues.Name;
- _member.PhoneNumber = membervalues.PhoneNumber;
- db.tblMembers.InsertOnSubmit(_member);
- db.SubmitChanges();
- return View();
- }
- }
- }
Code ContactForm.cshtml
- @model ClubMember.Models.MemberModel
- @{
- ViewBag.Title ="ContactForm" ;
- }
- <h2>ContactForm</h2>
- @using (Html.BeginForm("ContactForm" , "Home" ,FormMethod.Post, new { enctype= "multipart/form-information" }))
- {
- @Html.AntiForgeryToken()
- <divclass = "form-horizontal" >
- <h4>MemberModel</h4>
- <hr />
- @Html.ValidationSummary(truthful , "" , new { @ form = "text-danger" })
- <divclass = "form-group" >
- @Html.LabelFor(model => model.Proper name, htmlAttributes:new { @ class = "command-characterization col-md-two" })
- <divgrade = "col-dr.-10" >
- @Html.EditorFor(model => model.Name,new { htmlAttributes = new { @ class = "course-control" } })
- @Html.ValidationMessageFor(model => model.Name,"" , new { @ course = "text-danger" })
- </div>
- </div>
- <divclass = "course-group" >
- @Html.LabelFor(model => model.PhoneNumber, htmlAttributes:new { @ class = "control-label col-md-2" })
- <divform = "col-physician-10" >
- @Html.EditorFor(model => model.PhoneNumber,new { htmlAttributes = new { @ course = "course-command" } })
- @Html.ValidationMessageFor(model => model.PhoneNumber,"" , new { @ form = "text-danger" })
- </div>
- </div>
- <divclass = "form-group" >
- @Html.LabelFor(model => model.ImagePath, htmlAttributes:new { @ form = "command-label col-doctor-two" })
- <divclass = "col-doctor-10" >
- <input blazon="file" proper noun= "ImageFile" required />
- </div>
- </div>
- <divclass = "form-group" >
- <divclass = "col-physician-offset-2 col-medico-ten" >
- <input type="submit" value= "Create" course = "btn btn-default" />
- </div>
- </div>
- </div>
- }
- <div>
- @Html.ActionLink("Back to Listing" , "Index" )
- </div>
- @section Scripts {
- @Scripts.Return("~/bundles/jqueryval" )
- }
Code MemberModel.cs
- using System;
- using Organisation.Collections.Generic;
- using System.ComponentModel;
- using Organisation.ComponentModel.DataAnnotations;
- using Arrangement.Linq;
- using System.Web;
- namespace ClubMember.Models
- {
- public course MemberModel
- {
- [DisplayName("Member Name" )]
- public string Proper name { get; set; }
- [DisplayName("Phone / Mobile Number" )]
- public string PhoneNumber { become; fix; }
- [DisplayName("Upload File" )]
- public string ImagePath { go; gear up; }
- public HttpPostedFileBase ImageFile { get; set; }
- }
- }
Now, you tin cheque in your SQL Server database table if the record is inserted or non.
Look here. The above record is inserted successfully.
Give thanks you .
Happy Coding.
Source: https://www.c-sharpcorner.com/article/asp-net-mvc-form-with-file-upload/
0 Response to "Automatic Upload After File Upload Change Vb.net Asp"
Post a Comment