MvcApplication2/Controllers/BulkUploadController.cs

(完整代码) 2KB

using MvcApplication2.Bussiness;
using MvcApplication2.Filters;
using MvcApplication2.Models;
using MvcApplication2.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class BulkUploadController : AsyncController
    {
        //
        // GET: /BulkUpload/
          [HeaderFooterFilter]
          [AdminFilter]
        public ActionResult Index()
        {
            return View(new FileUploadViewModel());
        }
          public async Task<ActionResult> Upload(FileUploadViewModel model)
          {
              int t1 = Thread.CurrentThread.ManagedThreadId;
              List<Employee> employees = await Task.Factory.StartNew<List<Employee>>(() => GetEmployee(model));
              int t2 = Thread.CurrentThread.ManagedThreadId;
              EmployeeBusinessLayer bal = new EmployeeBusinessLayer();
              bal.UploadEmployee(employees);
              return RedirectToAction("Index", "Employee");
          }
          [AdminFilter]
          [HeaderFooterFilter]
          public ActionResult UploadEm()
          {
              return View("Index",new FileUploadViewModel());
          }
          private List<Employee> GetEmployee(FileUploadViewModel model)
          {
              List<Employee> employees = new List<Employee>();
              StreamReader csvreader = new StreamReader(model.fileUpload.InputStream);
              csvreader.ReadLine(); // Assuming first line is header
              while (!csvreader.EndOfStream)
              {
                  var line = csvreader.ReadLine();
                  var values = line.Split(',');//Values are comma separated
                  Employee e = new Employee();
                  e.FirstName = values[0];
                  e.LastName = values[1];
                  e.Salary = int.Parse(values[2]);
                  employees.Add(e);
              }
              return employees;
          }
    }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。