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

#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

posted @ 2018-10-26 15:40  橘子拾光  阅读(8359)  评论(0编辑  收藏  举报