• 文件上传-获取上传文件路径


    #region 文件上传-获取上传文件路径
    public string FilePath()
    {
    DataTable dtStu = s1.select(" select * from Student_V where tblstudentid = " + studentID + "");
    string userName = "";
    if (dtStu.Rows.Count > 0)
    {
    userName = dtStu.Rows[0]["name"].ToString();
    }
    System.Web.HttpFileCollection _file = System.Web.HttpContext.Current.Request.Files;
    string path = "";
    //if (_file.Count > 0)
    if (_file[0].ContentLength > 0)
    {
    long size = _file[0].ContentLength;
    string type = _file[0].ContentType;
    string name = _file[0].FileName;
    string _tp = System.IO.Path.GetExtension(name);
    if (_tp.ToLower() == ".docx")
    {
    System.IO.Stream stream = _file[0].InputStream;
    string saveName = userName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + _tp;
    int length = (int)stream.Length;
    BinaryReader br = new BinaryReader(stream);
    FileStream fs;
    path = System.Web.HttpContext.Current.Server.MapPath(@"~/UpLoadFile/Student/" + saveName);
    //string path = @"../UpLoadFile/Tutors/" + saveName;
    fs = File.Create(path);
    fs.Write(br.ReadBytes(length), 0, length);
    br.Close();
    fs.Close();
    //保存数据表中的路径
    path = @"../UpLoadFile/Student/" + saveName;
    }
    else
    {
    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择文件类型为.docx的文件');window.location.href=window.location.href;window.close();", true);
    }
    }
    else
    {
    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择文件');window.location.href=window.location.href;window.close();", true);
    }

    return path;
    }
    #endregion

    反思 空杯 担当
  • 相关阅读:
    JVM系列六(自定义插入式注解器).
    JVM系列五(Javac 字节码编译器).
    2019 — 求不得,放不下
    Mybatis 条件判断单双引号解析问题
    JVM系列四(对象分配策略).
    JVM系列三(垃圾收集器).
    Spring MVC -- Spring Tool Suite和Maven(安装Tomcat、JDK)
    Spring MVC -- 单元测试和集成测试
    Spring MVC -- 下载文件
    Spring MVC -- 上传文件
  • 原文地址:https://www.cnblogs.com/oralig/p/9856689.html
Copyright © 2020-2023  润新知