zoukankan      html  css  js  c++  java
  • 上传文件用户控件

    代码
    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 Admin_UploadFile : System.Web.UI.UserControl
    {
        
    private string uppaths = "../uploads";//必须存在
        private int uptype = 2;
        
    private int uplimite = 100;
        
    private string filetype = "jpg|gif|png|bmp";
        
    private string cssclass = "";
        
    private string filepath = "";

        
    // 上传路径
        public string upPaths
        
    {
            
    get return uppaths; }
            
    set { uppaths = value; }
        }

        
    //上传类型(1:原文件名 2:日期时间)
        public string upType
        
    {
            
    get return uptype.ToString(); }
            
    set { uptype = Convert.ToInt32(value); }
        }

        
    //限制大小(KB)
        public string upLimite
        
    {
            
    get return uplimite.ToString(); }
            
    set { uplimite = Convert.ToInt32(value);}
        }

        
    //可上传文件类型(扩展名)
        public string fileType
        
    {
            
    get return filetype; }
            
    set { filetype = value; }
        }

        
    public string CssClass
        
    {
            
    get return cssclass; }
            
    set { cssclass = value; }
        }

        
    public string filePath
        
    {
            
    get return PicPath.Text; }
            
    set
            
    {
                filepath 
    = value;
                PicPath.Text 
    = filepath;
            }

        }


        
    protected void Page_Load(object sender, EventArgs e)
        
    {
            
    if (!IsPostBack)
            
    {
                UploadBox.CssClass 
    = cssclass;
            }

        }


        
    protected void Button1_Click(object sender, EventArgs e)
        
    {
            
    string fullname;
            fullname 
    = FileUpload1.FileName.ToString();
            
    string url = FileUpload1.PostedFile.FileName.ToString();//这个是以前2003用的,先取得全部的上传文件路径个名字,然后再利用SubString方法来得到用户名,现在看来是没有必要了
            string typ = FileUpload1.PostedFile.ContentType.ToString();//获取文件MIME内容类型
            string typ2 = fullname.Substring(fullname.LastIndexOf("."+ 1);//获取文件名字 . 后面的字符作为文件类型
            long size = FileUpload1.PostedFile.ContentLength / 1024;
            
    int i = 0;
            
    if (uptype != 1)
            
    {
                fullname 
    = System.DateTime.Now.ToString("yyyyMMddhhmmss"+ "." + typ2;
            }

            
    string[] sArray = filetype.Split('|');
            
    foreach (string s in sArray)
            
    {
                
    if (s == typ2)
                
    {
                    i 
    = i + 1;
                }

            }

            
    if (i > 0)
            
    {
                
    if (uplimite >= size)
                
    {
                    FileUpload1.SaveAs(Server.MapPath(uppaths) 
    + "\\" + fullname);//将文件保存在跟目录的UP文件夹下
                    PicPath.Text = uppaths + @"/" + fullname;
                    Utility.jsUtility.Alert(
    "上传成功!");
                }

                
    else
                
    {

                    Utility.jsUtility.Alert(
    "您上传的文件不能超过 " + uplimite.ToString() + " KB!");
                }

            }

            
    else
            
    {
                Utility.jsUtility.Alert(
    "文件类型错误!支持类型:" + filetype);
            }

        }

    }


    界面

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UploadFile.ascx.cs" Inherits="Admin_UploadFile" %>
    <asp:Panel ID="UploadBox" runat="server">
    上传图片:
    <asp:FileUpload ID="FileUpload1" runat="server" />&nbsp;
    <asp:Button ID="Button1" runat="server" Text="开始上传" OnClick="Button1_Click" />
    <br />
    图片路径:
    <asp:TextBox ID="PicPath" runat="server" Width="280px" />
    </asp:Panel>
  • 相关阅读:
    iOS 类知乎”分页”效果的实现?
    iOS 图解弹幕功能的实现
    iOS 为何使用runtime方法交换多次后却能按照交换顺序依次执行代码逻辑?
    iOS常用算法之单链表查找倒数第n个节点(图解)
    iOS常用算法之两个有序数组合并, 要求时间复杂度为0(n)
    iOS 常用算法之设计一个算法,验证某字符是否为合法IPV4字符
    iOS .Crash文件分析处理办法 (利用symbolicatecrash工具处理)
    iOS中UIWebview中网页宽度自适应的问题
    iOS开发
    安卓应用加固之反动态调试技术总结
  • 原文地址:https://www.cnblogs.com/yeagen/p/1332134.html
Copyright © 2011-2022 走看看