question refined - Copy file from client to server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I asked the question yesterday, but know better how to ask it, today:

I'm trying to use the File.Copy method to copy a file from a client to
server (.Net web app under IIS [IUSR account]). It looks to me that when I
give a path like @"C:\holdfiles\myfile.txt" it looks on the server C drive.
How do I pull from the client? Do I need a different class and/or method?
Filestream?
 
When you are on the server, you cannot access the client's machine. The
server returns a bunch of HTML and script to the clientn (all text), the
client's browser then renders it all. All the server can ever do is send
that HTML and script in plain text, and then it's up to the client to render
the page.

Now, scripts will not have the necessary security rights to access files
directly on the client's machine - this would be a major security hole.

There are ActiveX controls, etc, you can write and have the client install,
but it involves the user installing it and giving it the rights it needs to
access the client machine.

I recommend you read up on how web servers work, how what they do interacts
with the client, etc.
 
We already bring back files from the client to load into SQL using
filestreams and streamwriters. Maybe we both need to read up.
--
Thanks,

CGW


Marina Levit said:
When you are on the server, you cannot access the client's machine. The
server returns a bunch of HTML and script to the clientn (all text), the
client's browser then renders it all. All the server can ever do is send
that HTML and script in plain text, and then it's up to the client to render
the page.

Now, scripts will not have the necessary security rights to access files
directly on the client's machine - this would be a major security hole.

There are ActiveX controls, etc, you can write and have the client install,
but it involves the user installing it and giving it the rights it needs to
access the client machine.

I recommend you read up on how web servers work, how what they do interacts
with the client, etc.

CGW said:
I asked the question yesterday, but know better how to ask it, today:

I'm trying to use the File.Copy method to copy a file from a client to
server (.Net web app under IIS [IUSR account]). It looks to me that when I
give a path like @"C:\holdfiles\myfile.txt" it looks on the server C
drive.
How do I pull from the client? Do I need a different class and/or method?
Filestream?
 
You are telling me that you have a web application that uses server side
code to get files sitting on the client computer of the person browing to
your .aspx page using the classes in the System.IO namespace?

In this case, I would love to see some sample code that can do this.

CGW said:
We already bring back files from the client to load into SQL using
filestreams and streamwriters. Maybe we both need to read up.
--
Thanks,

CGW


Marina Levit said:
When you are on the server, you cannot access the client's machine. The
server returns a bunch of HTML and script to the clientn (all text), the
client's browser then renders it all. All the server can ever do is send
that HTML and script in plain text, and then it's up to the client to
render
the page.

Now, scripts will not have the necessary security rights to access files
directly on the client's machine - this would be a major security hole.

There are ActiveX controls, etc, you can write and have the client
install,
but it involves the user installing it and giving it the rights it needs
to
access the client machine.

I recommend you read up on how web servers work, how what they do
interacts
with the client, etc.

CGW said:
I asked the question yesterday, but know better how to ask it, today:

I'm trying to use the File.Copy method to copy a file from a client to
server (.Net web app under IIS [IUSR account]). It looks to me that
when I
give a path like @"C:\holdfiles\myfile.txt" it looks on the server C
drive.
How do I pull from the client? Do I need a different class and/or
method?
Filestream?
 
Here you go... here's one that uploads an Excel file using an input type of
file on the ASPX page:

DataSet ds = new DataSet();

HttpPostedFile file = flFile.PostedFile;

// Make sure file is a valid Excel Spreadsheet
if (file.ContentType != "application/vnd.ms-excel")
{
spMessages.InnerHtml = "Please upload a valid Excel Spreadsheet!";
return;
}

int lastSlash = file.FileName.LastIndexOf("\\");

DateTime dTime = DateTime.Now;

excelFileName = dTime.DayOfYear.ToString() + dTime.Hour.ToString() +
dTime.Minute.ToString() + dTime.Second.ToString() +
dTime.Millisecond.ToString();

excelFileName += file.FileName.Substring((lastSlash + 1),
(file.FileName.Length - (lastSlash + 1)));
_excelUploadPath += excelFileName;

csvFileName = excelFileName.Replace("xls", "csv");
_csvUploadPath += csvFileName;

int fileLength = file.ContentLength;
byte[] fileData = new byte[fileLength];

// Read file contents into fileData byte[]
file.InputStream.Read(fileData, 0, fileLength);

// Upload Excel file
FileStream fs;
try
{
fs = new FileStream(_excelUploadPath, FileMode.Create);
fs.Write(fileData, 0, fileLength);
fs.Flush();
fs.Close();
}
catch (Exception err)
{
spMessages.InnerHtml = "An error occurred attempting to upload
file!<br>" + err.ToString();
return;
}

// Create a datasource for the dataset
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + _excelUploadPath + ";" +
"Extended Properties=Excel 8.0;";

OleDbConnection objConn = new OleDbConnection(sConnectionString);

try
{
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" +
worksheetName + "]", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
objAdapter.Fill(ds);
}
catch (Exception e)
{
ErrorHelper.HandleDBError("eventUpload.aspx.cs", "CreateCSV()", e);
}
finally
{
if (objConn.State == ConnectionState.Open)
objConn.Close();

File.Delete(_excelUploadPath);
}

--
Thanks,

CGW


Marina Levit said:
You are telling me that you have a web application that uses server side
code to get files sitting on the client computer of the person browing to
your .aspx page using the classes in the System.IO namespace?

In this case, I would love to see some sample code that can do this.

CGW said:
We already bring back files from the client to load into SQL using
filestreams and streamwriters. Maybe we both need to read up.
--
Thanks,

CGW


Marina Levit said:
When you are on the server, you cannot access the client's machine. The
server returns a bunch of HTML and script to the clientn (all text), the
client's browser then renders it all. All the server can ever do is send
that HTML and script in plain text, and then it's up to the client to
render
the page.

Now, scripts will not have the necessary security rights to access files
directly on the client's machine - this would be a major security hole.

There are ActiveX controls, etc, you can write and have the client
install,
but it involves the user installing it and giving it the rights it needs
to
access the client machine.

I recommend you read up on how web servers work, how what they do
interacts
with the client, etc.

I asked the question yesterday, but know better how to ask it, today:

I'm trying to use the File.Copy method to copy a file from a client to
server (.Net web app under IIS [IUSR account]). It looks to me that
when I
give a path like @"C:\holdfiles\myfile.txt" it looks on the server C
drive.
How do I pull from the client? Do I need a different class and/or
method?
Filestream?
 
You are not using a FileStream to actually get the data from the client's
machine. You are not opening up the filestream directly to the client's
file.

The user is uploading the file, and you are getting the actual data because
the user browsed to the file so your page could upload it. The user
willingly opened the dialog, chose the file, then clicked a button to send
the file to the server. Then you are just taking that uploaded file, and
doing something with it on the server.

This is nothing like wanting to have server side code that just goes to a
given client file, and does something with it.

This brings me back to my original point of the server not being able to
just manipulate the files on a client machine at will.

CGW said:
Here you go... here's one that uploads an Excel file using an input type
of
file on the ASPX page:

DataSet ds = new DataSet();

HttpPostedFile file = flFile.PostedFile;

// Make sure file is a valid Excel Spreadsheet
if (file.ContentType != "application/vnd.ms-excel")
{
spMessages.InnerHtml = "Please upload a valid Excel Spreadsheet!";
return;
}

int lastSlash = file.FileName.LastIndexOf("\\");

DateTime dTime = DateTime.Now;

excelFileName = dTime.DayOfYear.ToString() + dTime.Hour.ToString() +
dTime.Minute.ToString() + dTime.Second.ToString() +
dTime.Millisecond.ToString();

excelFileName += file.FileName.Substring((lastSlash + 1),
(file.FileName.Length - (lastSlash + 1)));
_excelUploadPath += excelFileName;

csvFileName = excelFileName.Replace("xls", "csv");
_csvUploadPath += csvFileName;

int fileLength = file.ContentLength;
byte[] fileData = new byte[fileLength];

// Read file contents into fileData byte[]
file.InputStream.Read(fileData, 0, fileLength);

// Upload Excel file
FileStream fs;
try
{
fs = new FileStream(_excelUploadPath, FileMode.Create);
fs.Write(fileData, 0, fileLength);
fs.Flush();
fs.Close();
}
catch (Exception err)
{
spMessages.InnerHtml = "An error occurred attempting to upload
file!<br>" + err.ToString();
return;
}

// Create a datasource for the dataset
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + _excelUploadPath + ";" +
"Extended Properties=Excel 8.0;";

OleDbConnection objConn = new OleDbConnection(sConnectionString);

try
{
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" +
worksheetName + "]", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
objAdapter.Fill(ds);
}
catch (Exception e)
{
ErrorHelper.HandleDBError("eventUpload.aspx.cs", "CreateCSV()", e);
}
finally
{
if (objConn.State == ConnectionState.Open)
objConn.Close();

File.Delete(_excelUploadPath);
}

--
Thanks,

CGW


Marina Levit said:
You are telling me that you have a web application that uses server side
code to get files sitting on the client computer of the person browing to
your .aspx page using the classes in the System.IO namespace?

In this case, I would love to see some sample code that can do this.

CGW said:
We already bring back files from the client to load into SQL using
filestreams and streamwriters. Maybe we both need to read up.
--
Thanks,

CGW


:

When you are on the server, you cannot access the client's machine.
The
server returns a bunch of HTML and script to the clientn (all text),
the
client's browser then renders it all. All the server can ever do is
send
that HTML and script in plain text, and then it's up to the client to
render
the page.

Now, scripts will not have the necessary security rights to access
files
directly on the client's machine - this would be a major security
hole.

There are ActiveX controls, etc, you can write and have the client
install,
but it involves the user installing it and giving it the rights it
needs
to
access the client machine.

I recommend you read up on how web servers work, how what they do
interacts
with the client, etc.

I asked the question yesterday, but know better how to ask it, today:

I'm trying to use the File.Copy method to copy a file from a client
to
server (.Net web app under IIS [IUSR account]). It looks to me that
when I
give a path like @"C:\holdfiles\myfile.txt" it looks on the server C
drive.
How do I pull from the client? Do I need a different class and/or
method?
Filestream?
 
Well then, please forgive me because I didn't state my question correctly...

The user will still choose the file. What I need to do is to copy that file
to a directory on the server, rather than load it into SQL. And THAT is my
question. How to do that best.
--
Thanks,

CGW


Marina Levit said:
You are not using a FileStream to actually get the data from the client's
machine. You are not opening up the filestream directly to the client's
file.

The user is uploading the file, and you are getting the actual data because
the user browsed to the file so your page could upload it. The user
willingly opened the dialog, chose the file, then clicked a button to send
the file to the server. Then you are just taking that uploaded file, and
doing something with it on the server.

This is nothing like wanting to have server side code that just goes to a
given client file, and does something with it.

This brings me back to my original point of the server not being able to
just manipulate the files on a client machine at will.

CGW said:
Here you go... here's one that uploads an Excel file using an input type
of
file on the ASPX page:

DataSet ds = new DataSet();

HttpPostedFile file = flFile.PostedFile;

// Make sure file is a valid Excel Spreadsheet
if (file.ContentType != "application/vnd.ms-excel")
{
spMessages.InnerHtml = "Please upload a valid Excel Spreadsheet!";
return;
}

int lastSlash = file.FileName.LastIndexOf("\\");

DateTime dTime = DateTime.Now;

excelFileName = dTime.DayOfYear.ToString() + dTime.Hour.ToString() +
dTime.Minute.ToString() + dTime.Second.ToString() +
dTime.Millisecond.ToString();

excelFileName += file.FileName.Substring((lastSlash + 1),
(file.FileName.Length - (lastSlash + 1)));
_excelUploadPath += excelFileName;

csvFileName = excelFileName.Replace("xls", "csv");
_csvUploadPath += csvFileName;

int fileLength = file.ContentLength;
byte[] fileData = new byte[fileLength];

// Read file contents into fileData byte[]
file.InputStream.Read(fileData, 0, fileLength);

// Upload Excel file
FileStream fs;
try
{
fs = new FileStream(_excelUploadPath, FileMode.Create);
fs.Write(fileData, 0, fileLength);
fs.Flush();
fs.Close();
}
catch (Exception err)
{
spMessages.InnerHtml = "An error occurred attempting to upload
file!<br>" + err.ToString();
return;
}

// Create a datasource for the dataset
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + _excelUploadPath + ";" +
"Extended Properties=Excel 8.0;";

OleDbConnection objConn = new OleDbConnection(sConnectionString);

try
{
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" +
worksheetName + "]", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
objAdapter.Fill(ds);
}
catch (Exception e)
{
ErrorHelper.HandleDBError("eventUpload.aspx.cs", "CreateCSV()", e);
}
finally
{
if (objConn.State == ConnectionState.Open)
objConn.Close();

File.Delete(_excelUploadPath);
}

--
Thanks,

CGW


Marina Levit said:
You are telling me that you have a web application that uses server side
code to get files sitting on the client computer of the person browing to
your .aspx page using the classes in the System.IO namespace?

In this case, I would love to see some sample code that can do this.

We already bring back files from the client to load into SQL using
filestreams and streamwriters. Maybe we both need to read up.
--
Thanks,

CGW


:

When you are on the server, you cannot access the client's machine.
The
server returns a bunch of HTML and script to the clientn (all text),
the
client's browser then renders it all. All the server can ever do is
send
that HTML and script in plain text, and then it's up to the client to
render
the page.

Now, scripts will not have the necessary security rights to access
files
directly on the client's machine - this would be a major security
hole.

There are ActiveX controls, etc, you can write and have the client
install,
but it involves the user installing it and giving it the rights it
needs
to
access the client machine.

I recommend you read up on how web servers work, how what they do
interacts
with the client, etc.

I asked the question yesterday, but know better how to ask it, today:

I'm trying to use the File.Copy method to copy a file from a client
to
server (.Net web app under IIS [IUSR account]). It looks to me that
when I
give a path like @"C:\holdfiles\myfile.txt" it looks on the server C
drive.
How do I pull from the client? Do I need a different class and/or
method?
Filestream?
 
You can't copy the file, because you only have access to the file in its
uploaded form - which is the stream.

You already wrote code in that snippet you posted to read this stream and
write it out to disk. So it looks to me like you already have this written.

CGW said:
Well then, please forgive me because I didn't state my question
correctly...

The user will still choose the file. What I need to do is to copy that
file
to a directory on the server, rather than load it into SQL. And THAT is my
question. How to do that best.
--
Thanks,

CGW


Marina Levit said:
You are not using a FileStream to actually get the data from the client's
machine. You are not opening up the filestream directly to the client's
file.

The user is uploading the file, and you are getting the actual data
because
the user browsed to the file so your page could upload it. The user
willingly opened the dialog, chose the file, then clicked a button to
send
the file to the server. Then you are just taking that uploaded file, and
doing something with it on the server.

This is nothing like wanting to have server side code that just goes to a
given client file, and does something with it.

This brings me back to my original point of the server not being able to
just manipulate the files on a client machine at will.

CGW said:
Here you go... here's one that uploads an Excel file using an input
type
of
file on the ASPX page:

DataSet ds = new DataSet();

HttpPostedFile file = flFile.PostedFile;

// Make sure file is a valid Excel Spreadsheet
if (file.ContentType != "application/vnd.ms-excel")
{
spMessages.InnerHtml = "Please upload a valid Excel Spreadsheet!";
return;
}

int lastSlash = file.FileName.LastIndexOf("\\");

DateTime dTime = DateTime.Now;

excelFileName = dTime.DayOfYear.ToString() + dTime.Hour.ToString() +
dTime.Minute.ToString() + dTime.Second.ToString() +
dTime.Millisecond.ToString();

excelFileName += file.FileName.Substring((lastSlash + 1),
(file.FileName.Length - (lastSlash + 1)));
_excelUploadPath += excelFileName;

csvFileName = excelFileName.Replace("xls", "csv");
_csvUploadPath += csvFileName;

int fileLength = file.ContentLength;
byte[] fileData = new byte[fileLength];

// Read file contents into fileData byte[]
file.InputStream.Read(fileData, 0, fileLength);

// Upload Excel file
FileStream fs;
try
{
fs = new FileStream(_excelUploadPath, FileMode.Create);
fs.Write(fileData, 0, fileLength);
fs.Flush();
fs.Close();
}
catch (Exception err)
{
spMessages.InnerHtml = "An error occurred attempting to upload
file!<br>" + err.ToString();
return;
}

// Create a datasource for the dataset
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + _excelUploadPath + ";" +
"Extended Properties=Excel 8.0;";

OleDbConnection objConn = new OleDbConnection(sConnectionString);

try
{
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" +
worksheetName + "]", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
objAdapter.Fill(ds);
}
catch (Exception e)
{
ErrorHelper.HandleDBError("eventUpload.aspx.cs", "CreateCSV()", e);
}
finally
{
if (objConn.State == ConnectionState.Open)
objConn.Close();

File.Delete(_excelUploadPath);
}

--
Thanks,

CGW


:

You are telling me that you have a web application that uses server
side
code to get files sitting on the client computer of the person browing
to
your .aspx page using the classes in the System.IO namespace?

In this case, I would love to see some sample code that can do this.

We already bring back files from the client to load into SQL using
filestreams and streamwriters. Maybe we both need to read up.
--
Thanks,

CGW


:

When you are on the server, you cannot access the client's machine.
The
server returns a bunch of HTML and script to the clientn (all
text),
the
client's browser then renders it all. All the server can ever do
is
send
that HTML and script in plain text, and then it's up to the client
to
render
the page.

Now, scripts will not have the necessary security rights to access
files
directly on the client's machine - this would be a major security
hole.

There are ActiveX controls, etc, you can write and have the client
install,
but it involves the user installing it and giving it the rights it
needs
to
access the client machine.

I recommend you read up on how web servers work, how what they do
interacts
with the client, etc.

I asked the question yesterday, but know better how to ask it,
today:

I'm trying to use the File.Copy method to copy a file from a
client
to
server (.Net web app under IIS [IUSR account]). It looks to me
that
when I
give a path like @"C:\holdfiles\myfile.txt" it looks on the
server C
drive.
How do I pull from the client? Do I need a different class and/or
method?
Filestream?
 
I'll be damned! I missed it. Thanks!
--
Thanks,

CGW


Marina Levit said:
You can't copy the file, because you only have access to the file in its
uploaded form - which is the stream.

You already wrote code in that snippet you posted to read this stream and
write it out to disk. So it looks to me like you already have this written.

CGW said:
Well then, please forgive me because I didn't state my question
correctly...

The user will still choose the file. What I need to do is to copy that
file
to a directory on the server, rather than load it into SQL. And THAT is my
question. How to do that best.
--
Thanks,

CGW


Marina Levit said:
You are not using a FileStream to actually get the data from the client's
machine. You are not opening up the filestream directly to the client's
file.

The user is uploading the file, and you are getting the actual data
because
the user browsed to the file so your page could upload it. The user
willingly opened the dialog, chose the file, then clicked a button to
send
the file to the server. Then you are just taking that uploaded file, and
doing something with it on the server.

This is nothing like wanting to have server side code that just goes to a
given client file, and does something with it.

This brings me back to my original point of the server not being able to
just manipulate the files on a client machine at will.

Here you go... here's one that uploads an Excel file using an input
type
of
file on the ASPX page:

DataSet ds = new DataSet();

HttpPostedFile file = flFile.PostedFile;

// Make sure file is a valid Excel Spreadsheet
if (file.ContentType != "application/vnd.ms-excel")
{
spMessages.InnerHtml = "Please upload a valid Excel Spreadsheet!";
return;
}

int lastSlash = file.FileName.LastIndexOf("\\");

DateTime dTime = DateTime.Now;

excelFileName = dTime.DayOfYear.ToString() + dTime.Hour.ToString() +
dTime.Minute.ToString() + dTime.Second.ToString() +
dTime.Millisecond.ToString();

excelFileName += file.FileName.Substring((lastSlash + 1),
(file.FileName.Length - (lastSlash + 1)));
_excelUploadPath += excelFileName;

csvFileName = excelFileName.Replace("xls", "csv");
_csvUploadPath += csvFileName;

int fileLength = file.ContentLength;
byte[] fileData = new byte[fileLength];

// Read file contents into fileData byte[]
file.InputStream.Read(fileData, 0, fileLength);

// Upload Excel file
FileStream fs;
try
{
fs = new FileStream(_excelUploadPath, FileMode.Create);
fs.Write(fileData, 0, fileLength);
fs.Flush();
fs.Close();
}
catch (Exception err)
{
spMessages.InnerHtml = "An error occurred attempting to upload
file!<br>" + err.ToString();
return;
}

// Create a datasource for the dataset
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + _excelUploadPath + ";" +
"Extended Properties=Excel 8.0;";

OleDbConnection objConn = new OleDbConnection(sConnectionString);

try
{
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" +
worksheetName + "]", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
objAdapter.Fill(ds);
}
catch (Exception e)
{
ErrorHelper.HandleDBError("eventUpload.aspx.cs", "CreateCSV()", e);
}
finally
{
if (objConn.State == ConnectionState.Open)
objConn.Close();

File.Delete(_excelUploadPath);
}

--
Thanks,

CGW


:

You are telling me that you have a web application that uses server
side
code to get files sitting on the client computer of the person browing
to
your .aspx page using the classes in the System.IO namespace?

In this case, I would love to see some sample code that can do this.

We already bring back files from the client to load into SQL using
filestreams and streamwriters. Maybe we both need to read up.
--
Thanks,

CGW


:

When you are on the server, you cannot access the client's machine.
The
server returns a bunch of HTML and script to the clientn (all
text),
the
client's browser then renders it all. All the server can ever do
is
send
that HTML and script in plain text, and then it's up to the client
to
render
the page.

Now, scripts will not have the necessary security rights to access
files
directly on the client's machine - this would be a major security
hole.

There are ActiveX controls, etc, you can write and have the client
install,
but it involves the user installing it and giving it the rights it
needs
to
access the client machine.

I recommend you read up on how web servers work, how what they do
interacts
with the client, etc.

I asked the question yesterday, but know better how to ask it,
today:

I'm trying to use the File.Copy method to copy a file from a
client
to
server (.Net web app under IIS [IUSR account]). It looks to me
that
when I
give a path like @"C:\holdfiles\myfile.txt" it looks on the
server C
drive.
How do I pull from the client? Do I need a different class and/or
method?
Filestream?
 
Back
Top