Master Page
Master Page

Reputation: 883

sqlBulk insert C#

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();
}

Upvotes: 0

Views: 117

Answers (3)

Andrew_tainton
Andrew_tainton

Reputation: 271

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.

Upvotes: 0

Gusman
Gusman

Reputation: 15151

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

Upvotes: 3

krillgar
krillgar

Reputation: 12805

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.

Upvotes: 2

Related Questions