C#上传文件或者图片

前台网页代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
   
    </div>
    </form>
</body>
</html>

 

后台上传事件控制代码:

 

  protected void Button1_Click(object sender, EventArgs e)
    {
        string filename=FileUpload1.FileName;
        string size = FileUpload1.PostedFile.ContentLength.ToString();
        string[] myfile = filename.Split('.');
        string dotname = myfile[myfile.Length - 1].ToString().ToLower();
        string type = FileUpload1.PostedFile.ContentType;
        string type2 = filename.Substring(filename.LastIndexOf(".") + 1);
        string imgpath = Server.MapPath("~/Upimg") + "\\";
        string filepath = Server.MapPath("~/Upfile") + "\\";
        string folder = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
        if (type2 == "jpg" || type2 == "png")
        {
            image1.Visible = true;
            if (!System.IO.Directory.Exists(imgpath + folder))
            {
                //自动生成文件夹
                System.IO.Directory.CreateDirectory(imgpath + folder);
            }
            Random myrdn = new Random(); //产生随机数
            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + myrdn.Next(10000).ToString() + "." + dotname;
            FileUpload1.SaveAs(imgpath + folder + "\\" + newfilename);
            string wpath = "~\\Upimg\\" + folder + "\\" + newfilename;
            image1.ImageUrl = wpath;
            Label1.Text = "原始文件名" + filename + "</br>存储文件名:" + newfilename + "</br>文件大小:" + size + "</br>文件类型:" + type2 + "</br>文件后缀" + type + "</br>文件虚拟路径:" + wpath;

        }
        else
        {
            if (!System.IO.Directory.Exists(filepath + folder))
            {
                System.IO.Directory.CreateDirectory(filepath + folder);
            }
            Random myrdn = new Random();//产生随机数

            //日期,时间,随机数和后缀名
            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + myrdn.Next(10000).ToString() + "." + dotname;

            image1.Visible = false;
            string wpath = "Upfile\\" + folder + newfilename;
            FileUpload1.SaveAs(filepath + folder + "\\" + newfilename);
            Label1.Text = "原始文件名" + filename + "<br>存储文件名:" + newfilename + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + type + "<br>文件虚拟路径" + wpath;


        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值