简体   繁体   中英

Read the first line of a file from memory

I have a tab delimited text file that I need to upload to a secure folder for SSIS to import into SQL Server. The file will be uploaded by an external user VIA a web app.

My challenge is that I need to check this file for some things before I am allowed to let it go to the secure folder. Namely I need to make sure the user has a specific column.

I can do this if I save the file to a folder in the web app. However the nature of the data in the file is such that we do not wish to place this file anywhere other then the secured folder. I also can not place it directly to this folder because the SSIS package is set to trigger as soon as the file shows up there.

What I need to do is find a way, if there is one, to parse the file in memory and if it passes all the checks, upload it to the secure folder.

I'm using C#.NET and the FileUpload Control.

My search so far has included all kinds of information but they all require saving the file somewhere first and then working with it.

Thank you so much for your time. If anybody can point me to an object or some code I can check out I would be most grateful.

Rather than calling SaveAs , use the FileContent property to access the contents of the file as a Stream , then you can do whatever processing is required before you save it manually.

For example, something like this:

string data;

using(StreamReader reader = new StreamReader(fileUpload.FileContent))
{
    data = reader.ReadToEnd();
}

The data variable now contains the contents of the file as a string. You can do whatever processing you like, then save it (or not) to the appropriate location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM