文件上传读存储器和文本文件中使用 - 有没有更好的办法? [英] File Upload read to memory and use as text file - is there a better way?
问题描述
我有一个内联网托管的Web应用程序,用户将上传空间分隔数据的文本文件中的5列。我不希望保存文件,所以我想只是用它在内存中。我试图从网上许多不同的例子,没有工作。最后,一位同事告诉我如何做到这一点。这里是code,我不知道是否有更好的方法来做到这一点。最后,我要的是对数据的GridView或中继器进行查看及更新的存储连接到一个数据库(SQL Server)的一种方式。
I have a intranet hosted web application where the user will upload a text file with space delimited data in 5 columns. I don't wish to save the file so I wanted to just use it in memory. I tried many different examples off the web and none worked. Finally a co-worker showed me how to do this. Here is the code and I'm wondering if there is a better way to do this. In the end, all I want is a way to link the data to a gridview or repeater for viewing and later storage into a database (SQL Server).
上传文件的asp标签ID是SurveyFileUpload结果
该SurveyDate是一个asp:输入字段
The upload file asp tag ID is SurveyFileUpload
The SurveyDate is an asp:input field
Int32 fileLen = SurveyFileUpload.PostedFile.ContentLength;
// Create a byte array to hold the contents of the file.
Byte[] buffer = new Byte[fileLen];
// Initialize the stream to read the uploaded file.
Stream s = SurveyFileUpload.FileContent;
// Read the file into the byte array.
s.Read(buffer, 0, fileLen);
// Convert byte array into characters.
ASCIIEncoding enc = new ASCIIEncoding();
string str = enc.GetString(buffer);
testReadFile(str, db_surveyDate.Text);
protected void testReadFile(string inFileString, string inSurveyDate)
{
string[] lines = inFileString.Split('\n');
curFileListing.InnerHtml = "";
int curRow = 1;
var readings = from line in lines
select new
{
// this is just for display purposes to show the number of rows on the page
Row = curRow++,
SurveyDate = inSurveyDate,
ItemNumber = Regex.Split(line, "[ ]+")[0],
Northing = Regex.Split(line, "[ ]+")[1],
Easting = Regex.Split(line, "[ ]+")[2],
Elevation = Regex.Split(line, "[ ]+")[3],
Name = Regex.Split(line, "[ ]+")[4]
};
saveFileData.Visible = true;
GridView fileData = new GridView();
fileData.DataSource = readings;
fileData.DataBind();
fileData.AlternatingRowStyle.BackColor =
System.Drawing.ColorTranslator.FromHtml("#eee");
curFileListing.Controls.Add(fileData);
}
该工程确定。我不是说LINQ知识渊博,我的日子不好过的文件流的部分。
This works OK. I'm not that knowledgeable with LINQ and I had a hard time with the file stream part.
推荐答案
我用这个方法。我的ID号从文本文件中读取每行一个ID号:
I use this method. I am reading in id numbers from a text file with one id number per line:
if (fileUpload.HasFile)
{
using (Stream fileStream = fileUpload.PostedFile.InputStream)
using (StreamReader sr = new StreamReader(fileStream))
{
string idNum = null;
while ((idNum = sr.ReadLine()) != null)
{
// Verify the line is in the expected id format
if (Regex.IsMatch(idNum, this.InputRegex))
{
// Do Stuff with input
}
else
{
Log.LogDebug("{0}Invalid input format.", LoggingSettings.logPrefix);
}
}
}
}
else
{
Log.LogDebug("{0}No file present.", LoggingSettings.logPrefix);
}
这篇关于文件上传读存储器和文本文件中使用 - 有没有更好的办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!