zoukankan      html  css  js  c++  java
  • 一个简单的文件上传(没有数据库的)

    protected void cmdUpload_Click(object sender, EventArgs e)
        {
            // Check if a file was submitted.
            if (Uploader.PostedFile.ContentLength != 0)
            {
                try
                {
                    if (Uploader.PostedFile.ContentLength > 1048576)
                    {
                        //这里限制文件的大小
                        //文件最大上传限于4MB
                        //如果还要更大,在web.conf文件中<httpRuntime>设置的maxRequestLength特性。这个设置以千字节为单位,所以<httpRuntime maxRequestLength="8192"/>把最大文件大小设为8MB。通过限制大小,可以防止试图占用web服务器硬盘的拒绝
                        lblStatus.Text = "Too large. This file is not allowed";
                    }
                    else
                    {
                        //上传到Upload路径下
                        string destDir = Server.MapPath("./Upload");

                        //获取文件名
                        string fileName = System.IO.Path.GetFileName(
                          Uploader.PostedFile.FileName);

                       
                        string destPath = System.IO.Path.Combine(destDir, fileName);

                        // 保存到服务端
                        Uploader.PostedFile.SaveAs(destPath);
                        lblStatus.Text += "Thanks for submitting your file.";

                        // 显示内容,如果文件过大,不要用ReadToEnd(),
                        StreamReader r = new StreamReader(Uploader.PostedFile.InputStream);
                        lblStatus.Text += r.ReadToEnd();
                        r.Close();
                    }
                }
                catch (Exception err)
                {
                    lblStatus.Text = err.Message;
                }
            }

        }

    我们可以创建全球唯一标识符

    private string GetFileName()
        {
            // Create a unique filename.
            string fileName = @"Log\user." +
                Guid.NewGuid().ToString();

            // Put the file in the current web application path.
            return Path.Combine(Request.PhysicalApplicationPath, fileName);
        }

  • 相关阅读:
    ZOJ 3765 Lights (zju March I)伸展树Splay
    UVA 11922 伸展树Splay 第一题
    UVALive 4794 Sharing Chocolate DP
    ZOJ 3757 Alice and Bod 模拟
    UVALive 3983 捡垃圾的机器人 DP
    UVA 10891 SUM游戏 DP
    poj 1328 Radar Installatio【贪心】
    poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】
    【转】RMQ-ST算法详解
    poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
  • 原文地址:https://www.cnblogs.com/gull/p/1874533.html
Copyright © 2011-2022 走看看