Monday 29 June 2015

Uploading files with Asp.net MVC

Uploading a single File:
Start with view having a form that will postback to the current action

<form action="" method="post" enctype="multipart/form-data">
  <label for="file">Filename:</label>
  <input type="file" name="file1" id="file1" />
  <input type="submit" />
</form>
 
In Controller
 
[HttpPost]
public ActionResult Index(HttpPostedFileBase file1) {

  if (file1.ContentLength > 0) {
    var fileName = Path.GetFileName(file1.FileName);
    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
    file1.SaveAs(path);
  }

  return RedirectToAction("Index");
} 


"~/App_Data/uploads" is a folder to upload files on server.
 
Uploading multiple files: 

In view 

<form action="" method="post" enctype="multipart/form-data">
  <label for="file1">File name:</label>
  <input type="file" name="files1" id="file1" />
  <label for="file2">File name:</label>
  <input type="file" name="files1" id="file2" />
  <input type="submit"  />
</form>
 
In Controller:
 
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
  foreach (var file1 in files) {
    if (file1.ContentLength > 0) {
      var fileName = Path.GetFileName(file1.FileName);
      var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
      file1.SaveAs(path);
    }
  }
  return RedirectToAction("Index");
} 
Here we are getting list of files so we are using IEnumerable to list the files.
So you can upload multiples files on server.

Monday 15 June 2015

Abstraction in OOPS C#

Abstraction: Abstraction is to hiding the complexity of object and showing the essential features of that object. It focus on what object can do instead of how it can do.
"Abstraction is to hiding the working style of an object and showing the information of an object as understandable manner"

Real world example of Abstraction:

Suppose we take example of Mobile phones.

Nokia 2710 (Features: Calling, SMS)
Nokia 3110 (Features: Calling, SMS, FM Radio, MP3, Camera)


Abstract information(All mobile phones makes a call to any number and can send SMS)

for that you have to make a abstract class:

abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }

public class Nokia2710 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }

    public class Nokia3110 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();
    }

Introduction to object oriented programing in C#

Class: A class is a blue print of an object that contains members and functions in it.

for ex:

class A
{
int a;
string s;
public void int sum(int a, int b
{
}
}

Object:An object is a instance of a class. You can create object of a class and access all the public variable and methods of that class.

for ex:

A objA=new Obj A();
int sum=a.sum(a,b);