独上高楼网站
  •    你所在位置:首页 VS.netASP.net编程经验〉如何上传文件到服务器
  • 如何上传文件到服务器
  • 作者:王宏喜  文章来源:自撰  发布日期:2007-05-26  浏览次数:1597
  • 打印这篇文章
  • 本程序已通过测试:

    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;

    public partial class filesUP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }
        protected void btnload_Click(object sender, EventArgs e)
        {
            myjoin p = new myjoin();
            string filePath="",fileExtName="",mFileName,mpath;
            System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息:


    ");
            if ("" != FileUP.PostedFile.FileName)
            {
                //取文件路径
                filePath = FileUP.PostedFile.FileName;
                //取得文件扩展名
                fileExtName = filePath.Substring(filePath.LastIndexOf(".")+1);
                try
                {
                    //取得与WEB服务器上的指定的虚拟路径相对应的文件路径
                    mpath = Server.MapPath("~/upfiles/");
                    //取得文件名
                    mFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);   //采用Substring字符串截取到最后一个“\\”后面的字符,就能截取掉路径,只取出文件名
                    //取得上传文件的信息
                    strMsg.Append("上传的文件类型:" + FileUP.PostedFile.ContentType.ToString() + "
    ");
                    strMsg.Append("客户端文件路径:" + FileUP.PostedFile.FileName + "
    ");
                    strMsg.Append("上传文件的文件名:" + mFileName + "
    ");
                    strMsg.Append("上传的文件扩展名:" + fileExtName);
                    //保存上传文件到指定目录
                    FileUP.PostedFile.SaveAs(mpath + mFileName);
                    strstate.Text = strMsg.ToString();
                }
                catch (Exception error)
                {
                    Response.Write(error.ToString());
                }
            }
        }
    }

     

  • 打印这篇文章
  • 与本文主题相关的文章
  • 返回首页