Automatic Upload After File Upload Change Vb.net Asp

Implementation Summary

  1. Create a "tblMembers" tabular array in your Database.
  2. Create a new ASP.Net MVC web application project called "ClubMember".
  3. Create a Model called "MemberModel".
  4. Create HTTPGET ActionMethod called "ContactForm".
  5. Create a view of "ContactForm".
  6. Use HTML File Upload control.
  7. Create HTTPPOST ActionMethod chosen "ContactForm" to insert record in tabular array and salve file in Server.
  8. Add Linq To Sql course "ClubMember".
  9. Insert a record into a database table.

Footstep past step Implementation

ASP.NET

In the above course, you can see there are four objects.

  1. Name Textbox
  2. Phone Number Textbox
  3. Image File Upload
  4. Submit Push button

Create a "tblMembers" tabular array in the database.

  1. Utilize [MbkTest]
  2. Become
  3. SET  ANSI_NULLS ON
  4. GO
  5. SET  QUOTED_IDENTIFIER ON
  6. GO
  7. Set  ANSI_PADDING ON
  8. Go
  9. CREATE TABLE  [dbo].[tblMembers](
  10.     [MemberID] [int ] IDENTITY(1,one) Non NULL ,
  11.     [MemberName] [varchar ](50) Nada ,
  12.     [PhoneNumber] [varchar ](50) NULL ,
  13.     [ImagePath] [varchar ](500) NULL ,
  14. Main Cardinal  Clustered
  15. (
  16.     [MemberID]ASC
  17. )WITH  (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON  [ Primary ]
  18. )ON  [ PRIMARY ]
  19. GO
  20. Prepare  ANSI_PADDING OFF
  21. Go

Create a new ASP.Cyberspace MVC project chosen "ClubMember".

ASP.NET

Click on "Alter Authentication".

ASP.NET

But we are looking for the following output.

ASP.NET

Click on "Change Authentication" push button and select No Authentication.

ASP.NET

ASP.NET

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).

ASP.NET

Requite Model Name: "MemberModel".

ASP.NET

Code of MemberModel.cs

  1. using System;
  2. using Organisation.Collections.Generic;
  3. using System.Linq;
  4. using Organization.Spider web;
  5. namespace ClubMember.Models
  6. {
  7. public course  MemberModel
  8.     {
  9.         public  string Name { get; set; }
  10. public  string PhoneNumber { get; prepare; }
  11. public  string ImagePath { go; set; }
  12. public  HttpPostedFile ImageFile { go; fix; }
  13.     }
  14. }

Now, build your project by right-clicking on the project and selecting BUILD.

ASP.NET

Now, switch to HOME Controller. Click on Controllers folder and double click on HOMECONTROLLER.CS file.

Create an action-method called CONTACTFORM.

ASP.NET

By default, the Add View dialog box volition display every bit below.

ASP.NET

Fill the "Add together VIEW" dialog box with the following values.

ASP.NET

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.

ASP.NET

Now, let us modify the CSHTML lawmaking.

Switch to CONTACTFORM.CSHTML file,  exercise the post-obit changes and printing F5.

ASP.NET

Remove following lawmaking of ImagePath

  1. @Html.EditorFor(model => model.ImagePath, new  { htmlAttributes = new  { @ form  = "form-control"  } })
  2. @Html.ValidationMessageFor(model => model.ImagePath,"" , new  { @ class  = "text-danger"  })

Add the following lines of lawmaking.

  1. <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

  1. using System;
  2. using System.Collections.Generic;
  3. using Organisation.ComponentModel;
  4. using Organisation.ComponentModel.DataAnnotations;
  5. using System.Linq;
  6. using System.Web;
  7. namespace ClubMember.Models
  8. {
  9. public class  MemberModel
  10.     {
  11.         [DisplayName("Member Name" )]
  12. public  cord Proper name { get; prepare; }
  13.         [DisplayName("Phone / Mobile Number" )]
  14. public  string PhoneNumber { get; set; }
  15.         [DisplayName("Upload File" )]
  16. public  string ImagePath { get; set; }
  17. public  HttpPostedFileBase ImageFile { get; set; }
  18.     }
  19. }

Note
DisplayName attribute comes after using the post-obit namespaces.

  1. using System.ComponentModel;
  2. using System.ComponentModel.DataAnnotations;

Now, again, switch to CONTACTFORM.CSHTML file, practise the following changes and press F5.

ASP.NET

Now, permit us switch over again to ContactForm.cshtml to modify the following.

Line No. 10

@using (Html.BeginForm())

Change this to:

  1. @using(Html.BeginForm( "ContactForm" , "Home" , FormMethod.Mail service, new
  2. {
  3.     enctype ="multipart/form-information"
  4. }))

Now, allow the states switch dorsum to the controller to piece of work on it.

ASP.NET

ASP.NET

  1. [HttpPost]
  2. public  ActionResult ContactForm(MemberModel membervalues)
  3. {
  4.     string FileName = Path.GetFileNameWithoutExtension(membervalues.ImageFile.FileName);
  5.     string FileExtension = Path.GetExtension(membervalues.ImageFile.FileName);
  6.     FileName = DateTime.Now.ToString("yyyyMMdd" )+ "-" +FileName.Trim()+ FileExtension;
  7.     string UploadPath = ConfigurationManager.AppSettings["UserImagePath" ].ToString();
  8.     membervalues.ImagePath = UploadPath + FileName;
  9.     membervalues.ImageFile.SaveAs(membervalues.ImagePath);
  10. return  View();
  11. }

After updating the HTTPPOST code, now, it is fourth dimension to create a new folder called "UserImages".

ASP.NET

You can see in the Solution Explorer folder that the "UserImages"  folder is created.

ASP.NET

Now, open web.config file to add APPSETTING.

AppSetting exists under Configuration Tag.

  1. <configuration>
  2.   <appSettings>
  3.     <add key="webpages:Version"  value= "3.0.0.0"  />
  4.     <add key="webpages:Enabled"  value= "false"  />
  5.     <add key="ClientValidationEnabled"  value= "truthful"  />
  6.     <add key="UnobtrusiveJavaScriptEnabled"  value= "true"  />
  7.     <add fundamental="UserImagePath"  value= "D:\MBK\ClubMember\ClubMember\UserImages\"  />
  8.   </appSettings>

In the higher up code, you tin can encounter UserImagePath key created with the value.

Now, fill the form.

ASP.NET

After clicking on CREATE button and submitting the form, you tin can see your selected file copied in the fix binder in web.config.

ASP.NET

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".

ASP.NET

Select Information--->LINQ to SQL Classes. Requite proper noun "ClubMemberDataClasses.dbml".

ASP.NET

Open ClubMemberDataClasses.dbml file and open Server Explorer.

ASP.NET

Click on the red button that is "Connect to database". Equally you click on this button, you lot will get the following dialog box.

ASP.NET

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.

ASP.NET

Now, drag and drib tblMembers within ClubMemberDataClasses.dbml.

ASP.NET

Now, switch back to HomeController to write INSERT record code into database table using LINQ TO SQL.

Code HomeController.cs

  1. using ClubMember.Models;
  2. using Arrangement;
  3. using System.Collections.Generic;
  4. using Arrangement.IO;
  5. using System.Linq;
  6. using System.Web;
  7. using Organisation.Web.Mvc;
  8. using Organisation.Configuration;
  9. namespace ClubMember.Controllers
  10. {
  11. public course  HomeController : Controller
  12.     {
  13. public  ActionResult Alphabetize()
  14.         {
  15. return  View();
  16.         }
  17. public  ActionResult About()
  18.         {
  19.             ViewBag.Bulletin ="Your awarding description page." ;
  20. return  View();
  21.         }
  22. public  ActionResult Contact()
  23.         {
  24.             ViewBag.Message ="Your contact page." ;
  25. return  View();
  26.         }
  27.         [HttpGet]
  28. public  ActionResult ContactForm()
  29.         {
  30. return  View();
  31.         }
  32.         [HttpPost]
  33. public  ActionResult ContactForm(MemberModel membervalues)
  34.         {
  35.             string FileName = Path.GetFileNameWithoutExtension(membervalues.ImageFile.FileName);
  36.             string FileExtension = Path.GetExtension(membervalues.ImageFile.FileName);
  37.             FileName = DateTime.Now.ToString("yyyyMMdd" )+ "-" +FileName.Trim()+ FileExtension;
  38.             string UploadPath = ConfigurationManager.AppSettings["UserImagePath" ].ToString();
  39.             membervalues.ImagePath = UploadPath + FileName;
  40.             membervalues.ImageFile.SaveAs(membervalues.ImagePath);
  41. var  db = new  ClubMemberDataClassesDataContext();
  42.             tblMember _member =new  tblMember();
  43.             _member.ImagePath = membervalues.ImagePath;
  44.             _member.MemberName = membervalues.Name;
  45.             _member.PhoneNumber = membervalues.PhoneNumber;
  46.             db.tblMembers.InsertOnSubmit(_member);
  47.             db.SubmitChanges();
  48. return  View();
  49.         }
  50.     }
  51. }

Code ContactForm.cshtml

  1. @model ClubMember.Models.MemberModel
  2. @{
  3.     ViewBag.Title ="ContactForm" ;
  4. }
  5. <h2>ContactForm</h2>
  6. @using (Html.BeginForm("ContactForm" , "Home" ,FormMethod.Post, new  { enctype= "multipart/form-information"  }))
  7. {
  8.     @Html.AntiForgeryToken()
  9.     <divclass = "form-horizontal" >
  10.         <h4>MemberModel</h4>
  11.         <hr />
  12.         @Html.ValidationSummary(truthful , "" , new  { @ form  = "text-danger"  })
  13.         <divclass = "form-group" >
  14.             @Html.LabelFor(model => model.Proper name, htmlAttributes:new  { @ class  = "command-characterization col-md-two"  })
  15.             <divgrade = "col-dr.-10" >
  16.                 @Html.EditorFor(model => model.Name,new  { htmlAttributes = new  { @ class  = "course-control"  } })
  17.                 @Html.ValidationMessageFor(model => model.Name,"" , new  { @ course  = "text-danger"  })
  18.             </div>
  19.         </div>
  20.         <divclass = "course-group" >
  21.             @Html.LabelFor(model => model.PhoneNumber, htmlAttributes:new  { @ class  = "control-label col-md-2"  })
  22.             <divform = "col-physician-10" >
  23.                 @Html.EditorFor(model => model.PhoneNumber,new  { htmlAttributes = new  { @ course  = "course-command"  } })
  24.                 @Html.ValidationMessageFor(model => model.PhoneNumber,"" , new  { @ form  = "text-danger"  })
  25.             </div>
  26.         </div>
  27.         <divclass = "form-group" >
  28.             @Html.LabelFor(model => model.ImagePath, htmlAttributes:new  { @ form  = "command-label col-doctor-two"  })
  29.             <divclass = "col-doctor-10" >
  30.                 <input blazon="file"  proper noun= "ImageFile"  required />
  31.             </div>
  32.         </div>
  33.         <divclass = "form-group" >
  34.             <divclass = "col-physician-offset-2 col-medico-ten" >
  35.                 <input type="submit"  value= "Create" course = "btn btn-default"  />
  36.             </div>
  37.         </div>
  38.     </div>
  39. }
  40. <div>
  41.     @Html.ActionLink("Back to Listing" , "Index" )
  42. </div>
  43. @section Scripts {
  44.     @Scripts.Return("~/bundles/jqueryval" )
  45. }

Code MemberModel.cs

  1. using System;
  2. using Organisation.Collections.Generic;
  3. using System.ComponentModel;
  4. using Organisation.ComponentModel.DataAnnotations;
  5. using Arrangement.Linq;
  6. using System.Web;
  7. namespace ClubMember.Models
  8. {
  9. public course  MemberModel
  10.     {
  11.         [DisplayName("Member Name" )]
  12. public  string Proper name { get; set; }
  13.         [DisplayName("Phone / Mobile Number" )]
  14. public  string PhoneNumber { become; fix; }
  15.         [DisplayName("Upload File" )]
  16. public  string ImagePath { go; gear up; }
  17. public  HttpPostedFileBase ImageFile { get; set; }
  18.     }
  19. }

ASP.NET

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.

ASP.NET

Give thanks you .

Happy Coding.

davisaferself.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel