ASP.NET MVC 壹:浏览器选择文件并上传到服务器
2021/11/10 14:39:58
本文主要是介绍ASP.NET MVC 壹:浏览器选择文件并上传到服务器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- 目的:在网页上选择需要的文件,并上传到IIS Express服务器。
- 文件结构:
- 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 - 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
这篇关于ASP.NET MVC 壹:浏览器选择文件并上传到服务器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#