There is a way to request a stream in C #, for example. select * from stream object

I have a file upload control on my asp.net website (C #) that is used to upload csv files with data to be inserted into the database, my problem is that I could not get the actual path of the downloaded file

It always gave me: C:\inetput\projectfolder\csvname.csv where it should have looked like: C:\Documents and settings\Pcname\Desktop\csvname.csv

but after going through various messages about downloading files, I found out that the file must first be saved on the server,

using Fileupload1.Saveas(savepath);

which is required to save the file in the previously specified location, where it is not actually required. (as this will increase the overhead of re-deleting the file).

what i am doing is as follows:

 bool result = true; strm = FileUpload1.PostedFile.InputStream; reader2 = new System.IO.StreamReader(strm); // **** new **** FileInfo fileinfo = new FileInfo(FileUpload1.PostedFile.FileName); string savePath = "c:\\"; // example "c:\\temp\\uploads\\"; could be any path string fileName = fileinfo.Name; string strFilePath = savePath.ToString(); savePath += fileName; FileUpload1.SaveAs(savePath); string strSql = "SELECT * FROM [" + fileinfo.Name + "]"; string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";" + "Extended Properties='text;HDR=NO;'"; // load the data from CSV to DataTable OleDbDataAdapter oleda = new OleDbDataAdapter(strSql, strCSVConnString); DataTable dtbCSV = new DataTable(); oleda.Fill(dtbCSV); if (dtbCSV.Columns.Count != 2) { result = false; } 

because I want to count the number of columns in a file, I use an oledb reader. which needs a file request.

Can I request a stream? I do not want to save the file, but just read it without saving.

+4
source share
1 answer

Unfortunately, you cannot use OLEDB for stream.

In situations where it is imperative to use OLEDB, I managed to write an IDisposable wrapper that would provide a temporary file from the stream and control the deletion.

However, you can use an alternative approach to reading content without saving it, for example, to independently parse the file directly from the stream. I would recommend this instead of a shell, since you avoid the problems of restricting access to files, as well as the overhead of accessing files.

Here are a few different approaches.

+1
source

All Articles