当前位置:编程学习 > C#/ASP.NET >>

这个是做文件上传的,我是新手,能帮我把代码解释的详细一点吗?谢谢各位了。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string fname;
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (UploadFile.Value != null && UploadFile.Value != "")
        {
            InputFile();
        }
        string sql;
        sql = "insert into ziyuanku(ziyuanmingcheng,ziyuanbao) values('" + TB1.Text.ToString().Trim() + "','" + fname + "')";
        int result;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["getcon"].ConnectionString);
        
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        result = cmd.ExecuteNonQuery();
        conn.Close();
  
        if (result == 1)
        {
            Response.Write("<script>javascript:alert('上传成功');</script>");
        }
        else
        {
            Response.Write("<script>javascript:alert('系统错误');</script>");
        }
    }
    private void InputFile()
    {
        if (UploadFile.Value != null)
        {
            string nam = UploadFile.PostedFile.FileName;
            int i = nam.LastIndexOf(".");
            string newext = nam.Substring(i);
            DateTime now = DateTime.Now;
            string newname = now.DayOfYear.ToString() + UploadFile.PostedFile.ContentLength.ToString();
            string filepath = UploadFile.PostedFile.FileName;
            string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
            fname = filename;

            FileInfo f = new FileInfo(filepath);
            string newpath = Server.MapPath("File") + "\\" + filename;
            f.CopyTo(newpath, true);

        }
    }

} --------------------编程问答-------------------- 这还用解释吗,一个是数据库操作,一个是UploadFile使用 --------------------编程问答-------------------- if (UploadFile.Value != null && UploadFile.Value != "")
  {
  InputFile();///处理上传文件
  }
  string sql;
  sql = "insert into ziyuanku(ziyuanmingcheng,ziyuanbao) values('" + TB1.Text.ToString().Trim() + "','" + fname + "')";//处理数据库,插入记录
  int result;
  SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["getcon"].ConnectionString);
   
  conn.Open();
  SqlCommand cmd = new SqlCommand(sql, conn);
  result = cmd.ExecuteNonQuery();//插入记录
  conn.Close();
  
  if (result == 1)
  {
  Response.Write("<script>javascript:alert('上传成功');</script>");
  }
  else
  {
  Response.Write("<script>javascript:alert('系统错误');</script>");
  }
  }
  private void InputFile()
  {
  if (UploadFile.Value != null)
  {
  string nam = UploadFile.PostedFile.FileName;
  int i = nam.LastIndexOf(".");
  string newext = nam.Substring(i);
  DateTime now = DateTime.Now;
  string newname = now.DayOfYear.ToString() + UploadFile.PostedFile.ContentLength.ToString();
  string filepath = UploadFile.PostedFile.FileName;
  string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
  fname = filename;

  FileInfo f = new FileInfo(filepath);
  string newpath = Server.MapPath("File") + "\\" + filename;
  f.CopyTo(newpath, true);//把上传的文件保存到newpath 下

  } --------------------编程问答-------------------- 关键是不理解代码是怎么用的 在这里代表什么意思啊 --------------------编程问答-------------------- 不理解很正常,一上来基础知识都没有,不是一两句话能解释的。

如同你没有上一年级、二年级,直接上三年级,当然什么也不懂,你以为老师给你解释下你就跟上了,哪有那么好的事情,那么那些念一年级二年级的不都是傻子了。

给你些思路:
你需要了解C#和HTML语言,了解ASP.NET 的基本原理,包括 Response Request 两大对象,了解 HTTP 协议和操纵文件流。了解SQL查询和数据库访问。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,