0

I have a page where I want to upload a CSV file from my computer to database on the server and I have my opentext that looks like the following

using (StreamReader sr = File.OpenText(@"c:\users\workstationUsername\FileName.csv"))

This works fine on my local machine but when I push this to the server it tries to read the server's C Drive and I want it to read the physical file location that is sitting on the desktop of the user's computer not the server, when they click browse and upload.. Thank you

below is the complete code:

if (IsPostBack)
{

   // SetDefaultDates();
   Boolean fileOK = false;
   String dateString = DateTime.Now.ToString("MMddyyyy");
   String UserName = User.Identity.Name;
   String path = Server.MapPath("~/Uploads/CSVs/");

   string stringpath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

   String fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
   stringpath = stringpath + fileName; 
   String LocationToSave = path + "\\" + fileName;
   if (FileUpload1.HasFile)
   {
       String fileExtension =
                System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
       String[] allowedExtensions = { ".csv" };
       for (int i = 0; i < allowedExtensions.Length; i++)
       {
           if (fileExtension == allowedExtensions[i])
           {
               fileOK = true;
           }
       }
   }

   if (fileOK)
   {
       try
       {
           //FileUpload1.PostedFile.SaveAs(LocationToSave + dateString + "-" + FileUpload1.FileName);
           FileUpload1.PostedFile.SaveAs(LocationToSave);
           Label1.Text = "File " + FileUpload1.FileName + " uploaded!";

           DataTable dt = new DataTable();
           string line = null;
           int i = 0;

           using (StreamReader sr = File.OpenText(stringpath)) 
           {
               while ((line = sr.ReadLine()) != null)
               {
                   string[] data = line.Split(',');
                   if (data.Length > 0)
                   {
                       if (i == 0)
                       {
                           foreach (var item in data)
                           {
                               dt.Columns.Add(new DataColumn());
                           }
                           i++;
                       }
                       DataRow row = dt.NewRow();
                       row.ItemArray = data;
                       dt.Rows.Add(row);
                   }
               }
           }

           using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString))
           {
               cn.Open();
               using (SqlBulkCopy copy = new SqlBulkCopy(cn))
               {
                   copy.WriteToServer(dt);
               }
           }
       }
       catch (Exception ex)
       {
           Label1.Text = "File " + FileUpload1.FileName + " could not be uploaded." + ex.Message;
       }
   }
   else
   {
       Label1.Text = "Cannot accept files of this type. " + FileUpload1.FileName;
   }
}
SetDefaultDates();
}
krillgar
  • 12,596
  • 6
  • 50
  • 86
Master Page
  • 823
  • 1
  • 6
  • 9

3 Answers3

3

If you have a FileUpload control, then instead of using (StreamReader sr = File.OpenText(@"c:\users\workstationUsername\FileName.csv")) which obvously is pointing to the server's hard drive you can do this:

(StreamReader sr = new StreamReader(fileUploadControl.FileContent))
    //Do your stuff
Gusman
  • 14,905
  • 2
  • 34
  • 50
2

You can't access the client's hard drive. That's a major security concern. You'll need to upload the file to your server, and read it from there.

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • You are not understanding my question or I am not making myself clear, I am not trying to access the client's Hard Drive but that's what it tries to do when I use the following string stringpath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) – Master Page Apr 25 '14 at 14:45
  • 1
    Then I'm still not clear on what you mean to do with "I want it to read the physical file location that is sitting on the desktop of the user's computer not the server" from your question. You can't hard code the file path in your code. You need to upload the file to your server to access it. Check out Gusman's answer, as that should be what you need. – krillgar Apr 25 '14 at 14:50
  • I just posted the complete code I have so you can understand what I wanted to explain. sorry if I explained it otherwise. – Master Page Apr 25 '14 at 14:54
0

It doesnt make sense to have a static read to the local machine, rather get user to upload it then update the database, this code is very limiting and has a high security risk. Rather create a steamreader object get the user to upload it then use the steam reader to process the csv.

Andrew_tainton
  • 261
  • 1
  • 3
  • 13
  • this is just an example I am doing exactly what you are describing above but the problem is when I push it to the server it tries to read the Server file location which I don't want it to do – Master Page Apr 25 '14 at 14:43