将文件中的文本直接上传到数据库 asp.net5 MVC 6

Uploading text from file directly in to Database asp.net5 MVC 6

我知道我得到一个 IFormFile 到我的控制器,将它保存在一个临时文件夹中,然后我使用 System.IO.File.ReadAllText 获取文件中的文本并将其存储在我的数据库中。之后我再次删除该文件。有没有办法可以跳过整个保存部分并直接从 IFormFile 中获取文本?

    [HttpPost]
    public ActionResult Upload(ViewModel model)
    {
        model.file.SaveAsAsync(@"c:\temp\temp.txt");
        String txt= System.IO.File.ReadAllText(@"c:\Temp\temp.txt ");
        db.saveText(txt);
        System.IO.File.Delete(@"c:\Temp\temp.txt ");

        return Redirect("/Home");
    }

提前致谢。

这可以使用 httpPostedFileBase class returns 指定的 HttpInputStreamObject 来完成 here

您应该将流转换为字节数组,然后才能读取文件内容

请参考以下link

http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx

希望对您有所帮助