0

I am trying to store file to uploads folder under App_Data folder

            HttpPostedFileBase file = files[i];
                    string extension = Path.GetExtension(file.FileName);

                    if (Path.GetExtension(file.FileName) == ".csv")
                    {

                        if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                        {
                            string fileName = file.FileName;
                            string fileContentType = file.ContentType;
                            byte[] fileBytes = new byte[file.ContentLength];

                            NameValueCollection nvc = new NameValueCollection();
                            nvc.Add("test", "teststr");
                            nvc.Add("sample", "samplestr");

                            // store the file inside ~/App_Data/uploads folder
                            var path = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/uploads"), fileName);
                            file.SaveAs(path);
    }
   }

When I run the application in Chrome and Firefox it works fine and var path returns me "D:\Sampleapp\webportal\SampleWeb\App_Data\uploads\ShoesImportExample.csv" which is correct.

But when I run the application in IE11 var path returns "C:\Users\sagar.j\Desktop\ShoesImportExample.csv" this is the path from where i selected the file.

So how can i get the path "D:\Sampleapp\webportal\SampleWeb\App_Data\uploads\ShoesImportExample.csv" in IE11.. Thanks in Advance..

Sagar Jagadesh
  • 237
  • 5
  • 14
  • Use `string fileName = Path.GetFileName(file.FileName);` –  May 16 '16 at 11:20
  • @StephenMuecke I want to store the file into uploads folder under App_Data folder. Ur code returns the path from where i selected the file.. – Sagar Jagadesh May 16 '16 at 11:23
  • No, it returns the file name (not the full path) and then use `var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);` –  May 16 '16 at 11:26
  • @StephenMuecke Already i am extracting file name in this line string fileName = file.FileName; and then i am trying var path = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/uploads"), fileName); but no use. – Sagar Jagadesh May 16 '16 at 11:28
  • You said in the question that your getting _the path from where i selected the file_? You need to use `.GetFileName()` to strip the full path and just return the file name –  May 16 '16 at 11:34
  • @StephenMuecke Thanks a lot, works like a charm.. – Sagar Jagadesh May 16 '16 at 12:09

0 Answers0