惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
H
Heimdal Security Blog
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Schneier on Security
G
GRAHAM CLULEY
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
P
Proofpoint News Feed
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
S
Security Affairs
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
N
News | PayPal Newsroom
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
NISL@THU
NISL@THU

博客园 - 星答

win7 小版本号16384( 最终测试版) 两个参数互换(不用第三个变量) c#实现九九乘法表 Excel导入导出 24券 域名 .net 6年之感想 服务器配置之DNS .net 杂记 asp.net 导出CSV Powerdesigner反向工程 常用时间函数 无任何网络提供程序接受指定的网络路径 虚拟机拖拽 sql表的所有者 奥运门票有了 阿泰斯特乘火箭 sql脚本导入Powerdesigner 'f_split' 不是可以识别的 函数名 加载App_Licenses出错
C# 上传文件 - 星答 - 博客园
星答 · 2008-05-30 · via 博客园 - 星答

webconfig 配置

<!--文件上传类型-->
  <add key="FileType" value=".doc,.xls,.txt,.rar"/>
  <add key="PicTureTye" value=".jpg|.gif|.png|.bmp|.psd|.svg|"/>
  <!--上传文件大小-->
  <add key="FileSizeLimit" value="102400"/>


    #region 判断上传文件类型
    protected bool IsAllowableFileType()
    {
        //从web.config读取判断文件类型限制
        string strFileTypeLimit = ConfigurationManager.AppSettings["FileType"].ToString();
        //当前文件扩展名是否包含在这个字符串中
        if (strFileTypeLimit.IndexOf(Path.GetExtension(FileUp.FileName).ToLower()) != -1)
        {
            return true;
        }
        else
            return false;
    }
    protected bool IsAllowablePictureType()
    {
        //从web.config读取判断图片类型限制
        string strFileTypeLimit = ConfigurationManager.AppSettings["PicTureTye"].ToString();
        //当前文件扩展名是否包含在这个字符串中
        if (strFileTypeLimit.IndexOf(Path.GetExtension(FileUp.FileName).ToLower()) != -1)
        {
            return true;
        }
        else
            return false;
    }
    #endregion

    #region 判断文件大小限制
    private bool IsAllowableFileSize()
    {
        //从web.config读取判断文件大小的限制
        double iFileSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FileSizeLimit"]) * 1024;
        //判断文件是否超出了限制
        if (iFileSizeLimit > FileUp.PostedFile.ContentLength)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    #endregion

  protected void btnUpFile_Click(object sender, EventArgs e)
    {
        
        //判读是否有上传文件
        if (FileUp.PostedFile.ContentLength > 0)
        {
            if (IsAllowableFileType())
            {
                if (Directory.Exists(Server.MapPath("~/File")) == false)//判断文件夹是否存在,若不存在则创建
                {
                    Directory.CreateDirectory(Server.MapPath("~/File"));
                }
                else
                    if (IsAllowableFileSize())
                    {
                        //string UploadFilePath = ConfigurationManager.AppSettings["UploadFile"].ToString();
                        string UploadFilePath = Server.MapPath("File\\");
                        string fullName = FileUp.PostedFile.FileName;
                        string newName = DateTime.Now.Ticks.ToString() + fullName.Substring(fullName.LastIndexOf("."));
                        FileUp.SaveAs(UploadFilePath + newName);
                        lblFileUrl.Text = fullName.Substring(fullName.LastIndexOf("\\")) + "   上传成功";
                        lblSaveFileName.Text = newName;
                    }
                    else
                        MessegeBox.Show(this, "文件太大了,上传失败");
            }
            else
                MessegeBox.Show(this, "文件类型不正确,上传失败");
        }
        else
        {
            MessegeBox.Show(this, "上传文件为空,上传失败");
        }

    }

到这里,文件已经上传到一个File文件夹中了,当然,如果要把路径保存到数据库,你需要另外写一个保存路径到数据库的方法,在此就不在赘述了。

如果有需要保存入数据库的方法,给我留言,我会回复.