views:

259

answers:

2

Hi, I would like to use HTML input file type in my aspx page to allow user to browse for a excel file and then read the content of the excel sheet programmatically.If I want to read the excel sheet I need the full path of the file to connect to the excel sheet using asp.net.I do not understand how can I get the full path of the file.

I know I can get the filename using postedFile.FileName property.But I need the full path of the file.

Could someone please help me with that.

Thanks.

A: 

There's no such notion as full path of the file. The server has no way of knowing where the file is stored on the client computer. All that you can know in your ASP.NET application is the filename and the contents of the uploaded file.

Darin Dimitrov
thanks for the reply.If that is the case is there a way I can allow the user to browse for a excel sheet and read the content of it programmatically using asp.net/C# ?
kranthi
Yes of course, you put a `FileUpload` inside your aspx page which allows the user to browse and select a file stored on his computer and upload it to the server when he submits the form. Once on the server you use the `Files` collection to read all posted file names and contents. You use `PostedFile.InputStream` which contains the stream of the file.
Darin Dimitrov
do u mean the asp.net fileupload control?If so is it not possible to achieve the same functionality using HTML input filetype?
kranthi
Yes I mean `asp:FileUpload` control. You can achieve the same functionality with an `<input type="file"` which is what the `FileUpload` control ends up writing into the HTML.
Darin Dimitrov
I am able to upload the file to server using HTML input filetype.when I upload the file to server I want to read the contents of the file into a dataset/datatable.is that possible using PostedFile.InputStream.Read or does it read the content only in bytes?
kranthi
It reads only in bytes but those bytes can depending on the format be converted to other types.
Darin Dimitrov
after reading the data in bytes I am trying to convert it into a datatable like this MemoryStream st = new MemoryStream(d); st.Position = 0; System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); dt=(DataTable)formatter.Deserialize(st);where 'd' refers to my byte array.
kranthi
But it throws me the following error.'Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.' Could you help me please?
kranthi
A: 

You can use the OpenXml SDK (http://msdn.microsoft.com/en-us/library/dd608815(office.14).aspx) to open the Excel sheet programatically, directly from the stream of the fileupload.

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;

FileUpload fu = (FileUpload)Page.FindControl("fuExcel");
using (SpreadsheetDocument uploadedWorkbook =
SpreadsheetDocument.Open(fu.FileContent, true))
{
    //interrogate the file here
}

The FileUpload control doesn't provide any information regarding the path where the file was uploaded from. You get:

  • The file's name (that is, abc.xyz, stripped of any path information), as FileName
  • A stream of the file content as FileContent
  • An array of bytes in the file as FileBytes
CuriousCoder