|
10金钱
protected void Button1_Click(object sender, EventArgs e)
{
string name, fl;
name = this.TextBox1.Text;
string filename = "";
if (fileId.PostedFile.ContentLength != 0)
{
string fn = fileId.PostedFile.FileName;
filename = getRandomFilename();
filename += fn.Substring(fn.LastIndexOf("."));
if ((fn.Substring(fn.LastIndexOf(".") + 1).ToLower() == "txt"))
{
if (fileId.PostedFile.ContentLength > 25000000)
{
Response.Write("<script>alert('不得上传大于25M的文件!')</script>");
}
else
{
fileId.PostedFile.SaveAs(Server.MapPath("~/txt") + "/" + filename);
}
}
else
{
Response.Write("<script> alert('" + fn.Substring(fn.LastIndexOf(".") + 1).ToLower() + "请选择格式为“txt”的文件')</script>");
return;
}
}
fl = "txt\\" + filename;
this.TextBox1.Text = fl;
string f0 = this.TextBox1.Text;
}
static public string getRandomFilename()
{
System.Random rand = new System.Random();
DateTime now = DateTime.Now;
string str = "";
str += now.Year.ToString();
str += now.Month.ToString();
str += now.Day.ToString();
str += now.Hour.ToString();
str += now.Minute.ToString();
str += now.Second.ToString();
str += rand.Next(0, 1000);
return str;
} 比如页面有 A B C D几个文本框,txt文件内容为:1 2 3 4 对应每一行,想通过上传txt文件之后读取文件内容到指定的文本框中,即读取内容1到文本框A,内容2到文本框B这样。
上面代码是将txt上传到服务器,求读取文件内容对应的代码,本人新手,不是编程专业,望大大帮忙,不胜感激!
|
|