Tuesday 10 October 2017

How to read the content of the file using Fileupload

 public RedirectResult UploadDictionary(FormCollection form)
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (StreamReader streamReader = new StreamReader(file.InputStream))
                        {
                            while (!streamReader.EndOfStream)
                            {
                                var dictionary = new FSDictionary();
                                dictionary.Word = streamReader.ReadLine().Trim().ToLower();
                                dictionary.PIDId = Convert.ToInt32(form["id"]);
                                dictionary.DateAdded = DateTime.Now;
                                dictionary.AddedBy = User.Identity.GetUserName();
                                session.Save(dictionary);
                            }
                        }

                    }
                }
            }

            return Redirect(Request.UrlReferrer.ToString());
        }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.