您的位置:首页 > 编程语言 > ASP

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

2017-02-20 13:59 851 查看
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 requestContent = Encoding.Default.GetString(byts);
string[] array = requestContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
int totalCount = array.Length; // 导入的记录总数
for (int i = 0; i < array.Length; i++)
{
//...拿到txt文件中的每一行数据,进而进行后续的逻辑
}
return View();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: