开发者

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

开发者 https://www.devze.com 2022-12-27 13:03 出处:网络
how to upload .txt files to sql database using asp.net(C开发者_如何学JAVA#)Is this something that will be done manually by a user?

how to upload .txt files to sql database using asp.net(C开发者_如何学JAVA#)


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();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号