你好,asp无组件上传文件怎么弄可以帮下我吗?谢谢!

[复制链接]
查看11 | 回复1 | 2011-4-20 09:03:27 | 显示全部楼层 |阅读模式
回复

使用道具 举报

千问 | 2011-4-20 09:03:27 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">protected void Button3_Click(object sender, EventArgs e)

{

string strfilepath = Path.GetFileName(this.FileUpload1.PostedFile.FileName);

if (strfilepath.Length0)

{

upfile();

Response.Redirect(Request.Url.ToString());

}

else

{

Label3.Text = \"上传文件不能为空\";

}

}


private void upfile()

{

string filepath = Server.MapPath(\"./\") \"file\";

HttpFileCollection hfc = Request.Files;

for (int i = 0; ihfc.Count; i)

{

HttpPostedFile userhpf = hfc;

try

{

if (userhpf.ContentLength0)

{

userhpf.SaveAs(filepath \"\\\\\" Path.GetFileName(userhpf.FileName));

SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings[\"ConnectionStr\"]);

con.Open();

SqlCommand cmd = new SqlCommand(\"insert into files values (@filename,@filedec,@filepath,@filetime)\", con);

cmd.Parameters.Add(\"@filename\", SqlDbType.VarChar, 50);

cmd.Parameters.Add(\"@filedec\", SqlDbType.VarChar, 200);

cmd.Parameters.Add(\"@filepath\", SqlDbType.VarChar, 100);

cmd.Parameters.Add(\"@filetime\", SqlDbType.DateTime);


cmd.Parameters[\"@filename\"].Value = TextBox1.Text;

cmd.Parameters[\"@filedec\"].Value = TextBox2.Text;

cmd.Parameters[\"@filepath\"].Value = filepath \"\\\\\" Path.GetFileName(userhpf.FileName);

cmd.Parameters[\"@filetime\"].Value = System.DateTime.Now;

cmd.ExecuteNonQuery();

con.Close();

}

}

catch

{

Label3.Text = \"上传失败\";

}

if (Session[\"FilesControls\"] != null)

{

Session.Remove(\"FilesControls\");

}

Label3.Text = \"上传成功\";

}

}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行