⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileupload.aspx.cs

📁 本程序基于 asp.net 2.0 +sql server 2
💻 CS
字号:
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;
using lalablog;
public partial class fileupload : System.Web.UI.Page
{
    string randFileName;
    string fullFileName; 

    protected void Page_Load(object sender, EventArgs e)
    {   
        String path = Server.MapPath("~/Uploadfiles/");
        randomTimeString rts = new randomTimeString();
        this.randFileName = rts.str+".jpg";
        this.fullFileName = path + randFileName;
        if (IsPostBack && FileUpload1.HasFile)   uploadimg();
    }

    private string  uploadimg()
    {
        Boolean fileOK = false;
       

        //检验是否指定后缀
        if (FileUpload1.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions =  { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                    fileOK = true;
            }
        //利用postedFile.ContentLength来判断文件长度,检查是否符合文件大小,250000就是250k字节
            if (fileOK && FileUpload1.PostedFile.ContentLength < 250000)
                fileOK = true;
            else
                fileOK = false;
        }

        if (fileOK)
        {
            try
            {
                FileUpload1.PostedFile.SaveAs(this.fullFileName );
                System.Drawing.Image upimg = System.Drawing.Image.FromFile(this.fullFileName);
                hfWidth .Value =upimg.Width.ToString ();

                if (Convert.ToInt32(hfWidth.Value) > 400)
                    hfWidth.Value = "400";

                hfName.Value = "uploadfiles/" + randFileName;
                Label2 .Text ="<img src='"+hfName .Value +"' width=120 height=90'/>";
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
        }
        else
        {
            Label1.Text = "无法上传的文件类型或文件大小超过250K";
        }
        return "";
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -