首页 > 学院 > 开发设计 > 正文

asp.net mvc中读取input file上传的txt文件内容,但不需要把文件保存到服务器上

2019-11-08 01:58:44
字体:
来源:转载
供稿:网友

asp.net mvc中读取input file上传的txt文件内容,但不需要把文件保存到服务器上:

view视图中的前台代码

@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" })){ <input type="file" name="file" id="fileUp" style="display:none;" /> <input type="button" id="btnUpload" value="选择文件" /> <input id="ButtonUpload" type="submit" value="提交" />}

controller中的action处理逻辑

[HttpPost] public ActionResult Upload() // 第一种方式 可以显示传入参数 HttpPostedFileBase file { var file = Request.Files[0]; // 第二种方式,在方法体内部手动接收 byte[] byts = new byte[file.InputStream.Length]; file.InputStream.Read(byts, 0, byts.Length); var reqContent = Encoding.Default.GetString(byts); reqContent = Server.UrlDecode(reqContent).Replace("/r/n", ","); string[] array = reqContent.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int totalCount = array.Length; // 导入的记录总数 for (int i = 0; i < array.Length; i++) { //...拿到txt文件中的每一行数据,进而进行后续的逻辑 } return View(); }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表