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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - 星答

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文件夹中了,当然,如果要把路径保存到数据库,你需要另外写一个保存路径到数据库的方法,在此就不在赘述了。

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