简体   繁体   English

从内存中读取文件的第一行

[英]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. 我有一个制表符分隔的文本文件,我需要将其上传到安全文件夹中,以便SSIS导入SQL Server。 The file will be uploaded by an external user VIA a web app. 该文件将由外部用户通过Web应用程序上传。

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. 如果将文件保存到Web应用程序的文件夹中,则可以执行此操作。 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. 我也不能将其直接放置到此文件夹中,因为SSIS包被设置为在文件显示在那里立即触发。

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. 我正在使用C#.NET和FileUpload控件。

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. FileContent调用SaveAs ,而是使用FileContent属性以Stream访问文件的内容,然后可以执行所需的任何处理,然后再手动保存。

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. 现在, data变量将文件内容包含为字符串。 You can do whatever processing you like, then save it (or not) to the appropriate location. 您可以执行所需的任何处理,然后将其保存(或不保存)到适当的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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