ASP.NET MVC 壹:浏览器选择文件并上传到服务器

这篇博客展示了如何在ASP.NET MVC应用中实现文件上传,特别是Excel文件,并进行数据验证和处理。用户通过网页选择Excel文件,然后提交到`Upload`控制器的`Upload`方法。该方法首先检查文件是否为空,再判断文件类型是否为.xlsx,最后将文件保存到指定目录,并尝试读取数据。虽然具体的数据处理部分被注释掉,但可以看出准备将数据导入到数据库中。
摘要由CSDN通过智能技术生成
  1. 目的:在网页上选择需要的文件,并上传到IIS Express服务器。
  2. 文件结构:

     

     

  3. Upload/Index.cshtml代码
     1 @using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
     2 {
     3     <div style="margin-top: 20px;">
     4         <fieldset id="myfieldset1">
     5             <legend>信息导入</legend>
     6             <p>选择Excel文件:<input id="FileUpload" type="file" name="files" style="width: 250px; height: 24px;background: White" class="easyui-validatebox" /></p>
     7             <p><input id="btnImport" type="submit" value="导入" style="width: 60px; height: 28px;" /></p>
     8             <p style="color: Red; text-align: center;">@ViewBag.error</p>
     9         </fieldset>
    10     </div>
    11 }
    
    ./Upload/Index.cshtml
  4. Upload/Upload代码
     1 public ActionResult Upload(HttpPostedFileBase httpPostedFileBase)
     2         {
     3             HttpPostedFileBase httpPostedFile = Request.Files["files"];
     4             string FileName, SavePath;
     5             if(httpPostedFile == null || httpPostedFile.ContentLength <= 0)
     6             {
     7                 ViewBag.error = "文件不能为空";
     8                 return View();
     9             }
    10             else
    11             {
    12                 string ExcelName = Path.GetFileName(httpPostedFile.FileName);
    13                 string Extention = System.IO.Path.GetExtension(ExcelName);
    14                 string NoExtention = Path.GetFileNameWithoutExtension(ExcelName);
    15                 string FileType = ".xlsx";
    16                 FileName = NoExtention + Extention;
    17                 if (!FileType.Contains(Extention))
    18                 {
    19                     ViewBag.error = "只能导入xlsx格式的文件";
    20                     return View();
    21                 }
    22                 string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
    23                 SavePath = Path.Combine(path, FileName);
    24                 httpPostedFile.SaveAs(SavePath);
    25             }
    26             string Result = string.Empty;
    27             string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + SavePath + ";" + "Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
    28             OleDbConnection oleDbConnection = new OleDbConnection(strConn);
    29             oleDbConnection.Open();
    30             OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter("select * from [生产计划$]", strConn);
    31             DataSet dataSet = new DataSet();
    32             try
    33             {
    34                 oleDbDataAdapter.Fill(dataSet, "Results");
    35                 oleDbConnection.Close();
    36                 DataTable dataTable = dataSet.Tables["Results"];
    37             }
    38             catch(InvalidCastException e)
    39             {
    40 
    41             }           
    42             SprayTable = dataSet.Tables["Results"].DefaultView.ToTable();
    43             //将部件数据放到数据库里
    44             //Spray spray = new Spray();
    45             //for(int i = 0; i < SprayTable.Rows.Count; i++)
    46             //{
    47             //    spray.MaterialGroupNum = SprayTable.Rows[i][20].ToString();
    48             //    spray.MaterialName = SprayTable.Rows[i][7].ToString().Replace("(", "(").Replace(")", ")");//不管用
    49             //    spray.PlanNum = Convert.ToInt32(SprayTable.Rows[i][12].ToString());
    50             //    layuiContext.Sprays.Add(spray);
    51             //    layuiContext.SaveChanges();
    52             //}
    53             return View(SprayTable);    //将excel表里的数据存储到了SprayTable里。
    54         }
    
    ./Controllers/UploadController.cs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值