zoukankan      html  css  js  c++  java
  • ASP.NET WEB开发,实现上传图片

    protected void btnUp_Click(object sender, EventArgs e)
        {
           
                Boolean fileOK = false;
                String path = Server.MapPath("~/UploadedImages/");
                if (fileUpLoad.HasFile)
                {
                    String fileExtension =
                        System.IO.Path.GetExtension(fileUpLoad.FileName).ToLower();
                    String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            fileOK = true;
                        }
                    }
                }
    
                if (fileOK)
                {
                    try
                    {
                        fileUpLoad.PostedFile.SaveAs(path
                            + fileUpLoad.FileName);
                        Label1.Text = "File uploaded!";
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = "File could not be uploaded." + ex.ToString();
                    }
                }
                else
                {
                    Label1.Text = "Cannot accept files of this type.";
                }
         
            
        }

    设计界面的代码如下

     <form id="form1" runat="server">
        <div>
        
            <asp:FileUpload ID="fileUpLoad" runat="server" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <p>
            <asp:Button ID="btnUp"
                runat="server" Text="上传" onclick="btnUp_Click" />
                </p>
        </div>
        </form>

    默认限制上传的大小为4M=4096

    在web.Config中进行设置,代码如下在<System.Web></System.Web>:

      <httpRuntime maxRequestLength="4096" executionTimeout="100"/>
  • 相关阅读:
    Python-常用的高级函数
    Excel
    业务思维
    数据分析思维
    Netbeans 12无法打开项目(project的)的问题
    C++ tuple元组
    如何保障一场千万级大型直播?
    回声消除的昨天、今天和明天
    无参考评估在云信的视频测试实践
    一文读懂Python 高阶函数
  • 原文地址:https://www.cnblogs.com/qiushuixizhao/p/3301238.html
Copyright © 2011-2022 走看看