uploadfile.aspx.cs

来自「数据结构管理系统 数据结构管理系统」· CS 代码 · 共 62 行

CS
62
字号
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;

public partial class Backtop_资料管理_UploadFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void BtnUpload_Click(object sender, EventArgs e)
    {
        string filepath1 = Server.MapPath("~/upload/") + FileUpload1.FileName;
        if (FileUpload1.HasFile == false)
        {
            Response.Write("<script>window.alert('你还没有选择文件!');</script>");
        }
        else
        {
            ///上传文件至网站的upload文件夹里
            string strName = FileUpload1.PostedFile.FileName.Trim();
            int intExt = strName.LastIndexOf(".");
            string strExt = strName.Substring(intExt);
            int intPath = strName.LastIndexOf("\\");
            string strNewName = strName.Substring(intPath);
            if (strExt != ".doc" && strExt != ".txt")
            {
                Response.Write("<script>window.alert('对不起,只能上传文本文档!');</script>");
            }
            else
            {
                try
                {
                    FileUpload1.SaveAs(filepath1);
                  
                    string filename = FileUpload1.FileName.Trim();
                    string newpath = "/数据结构学习系统/upload/" + FileUpload1.FileName.ToString();
                    //string type = FileUpload1.PostedFile.ContentType.Trim() + "( " + strExt + " )";
                    string value = (FileUpload1.PostedFile.ContentLength / 1024).ToString() + " KByte(s)";
                    int num = Convert.ToInt32(FileUpload1.PostedFile.ContentLength / 1024);

                    ///将文件信息写入数据库
                    File file = new File();
                    //File file = new File();
                    file.UploadFile(filename, newpath, value);
                    Response.Write("<script>window.alert('上传成功,谢谢!');</script>");
                }
                catch 
                {
                    throw new ApplicationException("上传失败!");
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?