WebForm實現檔案上傳,並預覽

來源:互聯網
上載者:User

標籤:eve   har   擷取   成功   onclick   doc   controls   資訊   pos   

實現效果:

頁面代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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></head><body>    <form id="form1" runat="server">        <div>            <asp:FileUpload ID="FileUpload1" runat="server" />            <asp:Button ID="Button1" runat="server" Text="上傳" Width="54px" OnClick="Button1_Click" />            <asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>            <asp:Image runat="server" ID="Image1" Style="z-index: 102; left: 20px; position: absolute; top: 49px"                Width="73px" />        </div>    </form></body></html>

後台代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }    protected void Button1_Click(object sender, EventArgs e)    {        if (FileUpload1.FileName == "")        {            this.Label1.Text = "上傳檔案不可為空";            return;        }        bool fileIsValid = false;        //如果確認了上傳檔案,則判斷檔案類型是否符合要求         if (this.FileUpload1.HasFile)        {            //擷取上傳檔案的尾碼             String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();            String[] restrictExtension = { ".gif", ".jpg", ".bmp", ".png" };            //判斷檔案類型是否符合要求             for (int i = 0; i < restrictExtension.Length; i++)            {                if (fileExtension == restrictExtension[i])                {                    fileIsValid = true;                }                //如果檔案類型符合要求,調用SaveAs方法實現上傳,並顯示相關資訊                 if (fileIsValid == true)                {                    //上傳檔案是否大於10M                    if (FileUpload1.PostedFile.ContentLength > (10 * 1024 * 1024))                    {                        this.Label1.Text = "上傳檔案過大";                        return;                    }                    try                    {                        //產生的檔案放在那個目錄下面                        this.Image1.ImageUrl = "~/File/" +  FileUpload1.FileName;                        this.FileUpload1.SaveAs(Server.MapPath("~/File/") + FileUpload1.FileName);                        this.Label1.Text = "檔案上傳成功!";                    }                    catch                    {                        this.Label1.Text = "檔案上傳失敗!";                    }                    finally                    {                    }                }                else                {                    this.Label1.Text = "只能夠上傳尾碼為.gif,.jpg,.bmp,.png的檔案";                }            }        }    }}

 

WebForm實現檔案上傳,並預覽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.