472,114 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,114 software developers and data experts.

question refined - Copy file from client to server

CGW
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?
--
Thanks,

CGW
Feb 8 '06 #1
9 2278
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" <CG*@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
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?
--
Thanks,

CGW

Feb 8 '06 #2
CGW
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
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?
--
Thanks,

CGW


Feb 8 '06 #3
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" <CG*@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
>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?
> --
> Thanks,
>
> CGW


Feb 8 '06 #4
CGW
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
>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?
> --
> Thanks,
>
> CGW


Feb 8 '06 #5
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" <CG*@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
> 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 [MVP]" wrote:
>
>> 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" <CG*@discussions.microsoft.com> wrote in message
>> news:27**********************************@microsof t.com...
>> >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?
>> > --
>> > Thanks,
>> >
>> > CGW
>>
>>
>>


Feb 8 '06 #6
CGW
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
> 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 [MVP]" wrote:
>
>> 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" <CG*@discussions.microsoft.com> wrote in message
>> news:27**********************************@microsof t.com...
>> >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?
>> > --
>> > Thanks,
>> >
>> > CGW
>>
>>
>>


Feb 8 '06 #7
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" <CG*@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
> 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 [MVP]" wrote:
>
>> 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" <CG*@discussions.microsoft.com> wrote in message
>> news:9D**********************************@microsof t.com...
>> > 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 [MVP]" wrote:
>> >
>> >> 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" <CG*@discussions.microsoft.com> wrote in message
>> >> news:27**********************************@microsof t.com...
>> >> >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?
>> >> > --
>> >> > Thanks,
>> >> >
>> >> > CGW
>> >>
>> >>
>> >>
>>
>>
>>


Feb 8 '06 #8
CGW
I'll be damned! I missed it. Thanks!
--
Thanks,

CGW
"Marina Levit [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
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 [MVP]" wrote:
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" <CG*@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
> 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 [MVP]" wrote:
>
>> 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" <CG*@discussions.microsoft.com> wrote in message
>> news:9D**********************************@microsof t.com...
>> > 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 [MVP]" wrote:
>> >
>> >> 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" <CG*@discussions.microsoft.com> wrote in message
>> >> news:27**********************************@microsof t.com...
>> >> >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?
>> >> > --
>> >> > Thanks,
>> >> >
>> >> > CGW
>> >>
>> >>
>> >>
>>
>>
>>


Feb 8 '06 #9
CGW
Found it. It was simple. The "SaveAs" method was all I needed.
--
Thanks,

CGW
"CGW" wrote:
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?
--
Thanks,

CGW

Feb 8 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by J Poirier | last post: by
27 posts views Thread by Gene Ellis | last post: by
7 posts views Thread by CT | last post: by
4 posts views Thread by Peter | last post: by
3 posts views Thread by CeJay | last post: by
5 posts views Thread by jehugaleahsa | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.