tags:

views:

66

answers:

1

Hello all.

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 gratefull.

  • George
+7  A: 

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.

Adam Robinson
Nitpicking: Since the subject line says, "Read the first line...", wouldn't `reader.ReadLine()` be more appropriate?
Tergiver
@Tergiver: The point was to demonstrate an example of obtaining the data delivered by the control for server-side processing. In this case, yes, `ReadLine` would likely be sufficient, but I have not tried combining both reading `FileContent` *and* calling `SaveAs`, so I assumed that the OP would read the file, process it, then save it directly.
Adam Robinson
I got it working. I used the following. StreamReader theFile = new StreamReader(strFilePath + strFileName); And then int counter = 0; String strOutput = ""; String line = ""; while ((line = theFile.ReadLine()) != null) ... Funny part was after asking more about the business requirements, saving the file to the web server first was acceptable.
George