文件的上传

这些是主要的代码

文件上传的

protected void 学生上传信息_upfile_Click(object sender, EventArgs e)//文件上传
    {
        学生上传信息.Text = upfile(学生上传信息_file, MapPath("..") + "/uploadfile/", "uploadfile/");
        教师下载信息.Text = 学生上传信息.Text;
    }

protected string upfile(FileUpload fp, string jfathhead, string xfathhead)
    {
        if (!fp.HasFile) { Response.Write("<script>alert('请选择文件!');</script>"); return "0"; }
        else
        {
            int index1 = fp.FileName.LastIndexOf(".") + 1;
            int index2 = fp.FileName.Length - index1;
            string lx = fp.FileName.Substring(index1, index2);//取文件类型
            // string time = System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString()
            //       + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString();
            //以时间作为文件ID,精确到微秒;
            string fileName = fp.PostedFile.FileName.Substring(fp.PostedFile.FileName.LastIndexOf("\\") + 1);


            string fullpath = jfathhead +学号.Text+姓名.Text+ 课程名称.Text +上传时间.Text+ "." + lx;//服务器文件绝对路径;
            string path = xfathhead + 学号.Text + 姓名.Text + 课程名称.Text + 上传时间.Text+"." + lx;//服务器文件相对路径;
            if (System.IO.File.Exists(fullpath))
            {
                string alert = "已上传过同名文件,请重新选择!";
                Response.Write(" <script> alert( '" + alert + " ') </script> ");
                Label2.Text = "已上传过同名文件,请重新选择!";
                return alert;
            }
            else
            {
                fp.PostedFile.SaveAs(fullpath);//上传,但是这个函数会覆盖同名文件,请慎用;
                Response.Write("<script>alert('上传成功!');</script>");
                Label2.Text = "当前材料信息:" + path;
                Label2.Visible = true;
                return path;
            }
        }
    }

原文地址:https://www.cnblogs.com/gull/p/1874555.html