如何使用asp.net将.txt文件上传到sql数据库(C#)

发布于 2024-08-28 23:51:00 字数 38 浏览 1 评论 0原文

如何使用asp.net将.txt文件上传到sql数据库(C#)

how to upload .txt files to sql database using asp.net(C#)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2024-09-04 23:51:00

这是由用户手动完成的事情吗?

如果是这样,那么 ASP.NET MVC 应用程序怎么样?

在您的 视图 中:

<h2>
    Upload A File of Foos</h2>
<%
    Html.BeginForm("LoadFoos", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" });
%>
<label for="foosFile">
    Foos file:
</label>
<input type="file" name="FileUpload1" id="foosFile" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
<%
    Html.EndForm();
%>

在您的 控制器

public class AdminController : Controller
{
    public ActionResult LoadFoos()
    {
        if (Request.Files.Count > 0)
        {
            // This illustrates how to read the uploaded file contents,
            // and would need to be adapted to your scenario
            List<string> foos = new List<string>();

            using (StreamReader reader = new StreamReader(Request.Files[0].InputStream))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();

                    foos.Add(line);
                }
            }

            // TODO: use LINQ 2 SQL, NHibernate or ADO.NET to load the foos directly,
            // or call a web/WCF service behind your firewall that does this
        }

        return View();
    }
}

Is this something that will be done manually by a user?

If so, how about an ASP.NET MVC application?

In your View:

<h2>
    Upload A File of Foos</h2>
<%
    Html.BeginForm("LoadFoos", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" });
%>
<label for="foosFile">
    Foos file:
</label>
<input type="file" name="FileUpload1" id="foosFile" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
<%
    Html.EndForm();
%>

In your Controller:

public class AdminController : Controller
{
    public ActionResult LoadFoos()
    {
        if (Request.Files.Count > 0)
        {
            // This illustrates how to read the uploaded file contents,
            // and would need to be adapted to your scenario
            List<string> foos = new List<string>();

            using (StreamReader reader = new StreamReader(Request.Files[0].InputStream))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();

                    foos.Add(line);
                }
            }

            // TODO: use LINQ 2 SQL, NHibernate or ADO.NET to load the foos directly,
            // or call a web/WCF service behind your firewall that does this
        }

        return View();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文