[RESOLVED]Reading CSV file for validation and after that saving it on server in MVC 4

Hi,

I have CSV file upload functionality in MVC4. Below my code is working fine on local machine, when I deploy same code on server I am getting error "Could not find file at C:WindowsSystem32InetSrv".

  [HttpPost]
        public ActionResult Upload(HttpPostedFileBase UploadedFile)
        {
            try
            {
                var fp = Path.GetFileName(UploadedFile.FileName);
                var reader = new StreamReader(UploadedFile.FileName);
                while (!reader.EndOfStream)
                {

                        string[] str = reader.ReadLine().Split(‘,’);  //Validation Logic.

                 }

                reader.Close();
                reader.Dispose();
                UploadedFile.SaveAs(@"d:FileUploadDemo" + fp);
                return View("Index");
            }
            catch (Exception)
            {
                return View("Index");
            }
        }

But If I comment below file reading code,

  var fp = Path.GetFileName(UploadedFile.FileName);
                var reader = new StreamReader(UploadedFile.FileName);
                while (!reader.EndOfStream)
                {
                    string[] str = reader.ReadLine().Split(‘,’);
                }
                reader.Close();
                reader.Dispose();

then my file is getting saved on the server perfectly fine.

But, Before saving a file, I want to validate the file data. I have implemented client side code but it is not working in IE browsers. So wanted to do server side validation.

Or any client side code, working in IE8+ browsers (without asking user to activate ActiveX object) is greatly appreciated.

The filename only contains the name of the file itself, it is for "information" purposes, the file isn’t actually saved to disc until you save it using SaveAs, so you can’t access it yet via StreamReader.  If you want to read its contents you need to access
the file data via the InputStream and convert it to a string that you can then validate

http://stackoverflow.com/questions/16030034/asp-net-mvc-read-file-from-httppostedfilebase-without-save

Leave a Reply