ASP.Net小文件上传

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

注意:拖拽控件 一个FileUpload、一个label,一个button,并添加一个imgs文件夹

UploadFile.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFile.aspx.cs" Inherits="UploadFile" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
     <script>
        function chgImg(file) {
            var prevDiv = document.getElementById("preview");
            if(file.files&&file.files[0]){
                var reader = new FileReader();
                reader.onload = function (evt) {
                    prevDiv.innerHTML = '<img src="' + evt.target.result + '"style="max-height:200px;"/>';
                }
                reader.readAsDataURL(file.files[0]);
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <asp:FileUpload ID="FileUpload1" runat="server" onchange="chgImg(this)" />
        <p>
            <asp:Label ID="Label1" runat="server" Text="图片预览"></asp:Label>
        </p>
        <div id="preview"></div>
        <p>
            <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
        </p>
    </form>
</body>
</html>

UploadFile.aspx.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class UploadFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bool filesValid = false;
        double mb = FileUpload1.PostedFile.ContentLength / 1024 / 1024.0;
        DateTime now = DateTime.Now;
        string path = "/imgs/";
        string fileName = "" + now.Year + now.Month + now.Day + now.Hour + now.Minute + now.Second + now.Millisecond;
        if (FileUpload1.HasFile)
        {
            String fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] restrictExtension = { ".jpg", ".jpeg", ".png" };
            for (int i = 0; i < restrictExtension.Length; i++)
            {
                if (fileExtension == restrictExtension[i] && mb <= 4)
                {
                    filesValid = true;
                }
            }
            if (filesValid == true)
            {
                try
                {
                    //Image1.ImageUrl = path+ fileName + fileExtension;
                    FileUpload1.SaveAs(Server.MapPath(path) + fileName + fileExtension);
                    Label1.Text = "上传成功";
                }
                catch
                {
                    Label1.Text = "上传失败";
                }
                finally
                {
                    //Image1.Dispose();
                }
            }
        }
        else
        {
            Label1.Text = "格式错误";
        }
    }
}

原文链接:https://www.idaobin.com/archives/1129.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

电子邮件地址不会被公开。 必填项已用*标注

87 + = 97