コード例 #1
0
 public ActionResult InsertMemData()
 {
     if (ModelState.IsValid)
     {
         //this.products.ForEach(p => db.Products.Add(p));
         //db.SaveChanges();
         var pH = new ProductsHandler();
         pH.SetProductsDB(products);
         return(RedirectToAction("Index", "EditProducts"));
     }
     else
     {
         return(View("Error")); // Todo: use error view to show error msg.
     }
 }
コード例 #2
0
        public ActionResult DoFileUpload(HttpPostedFileBase file)
        {
            //Undone: show error msg to user!!
            if (ModelState.IsValid)
            {
                if (file == null)
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                }
                else if (file.ContentLength > 0)
                {
                    int      MaxContentLength      = 1024 * 1024 * 20; //20 MB
                    string[] AllowedFileExtensions = new string[] { ".csv", ".txt" };

                    if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                    {
                        ModelState.AddModelError("File", "Please upload file of type: " + string.Join(", ", AllowedFileExtensions));
                    }

                    else if (file.ContentLength > MaxContentLength)
                    {
                        ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
                    }
                    else
                    {
                        //TO:DO
                        var fileName = Path.GetFileName(file.FileName);
                        var path     = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
                        file.SaveAs(path); //Note: overwrite file with the same name
                        ModelState.Clear();
                        ViewBag.Message = "File uploaded successfully";

                        //Todo: caution, overflow! long file, file.InputStream.Length is a long int
                        //var b = new BinaryReader(file.InputStream);
                        //byte[] binData = b.ReadBytes((int)file.InputStream.Length);
                        //string s = new StreamReader(file.InputStream).ReadToEnd();
                        var pH = new ProductsHandler();
                        pH.SetProductsDB(
                            pH.GetProductsStream(file.InputStream)
                            );

                        return(RedirectToAction("Index", "EditProducts"));
                    }
                }
            }
            return(View());
        }