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?