=L=圖片上傳整理

來源:互聯網
上載者:User

標籤:多個   exception   命名   controls   protected   檔案格式   last   log   onclick   

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Alum.aspx.cs" Inherits="Album_Default" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>    <asp:FileUpload ID="fup" runat="server" />        <br />        <br />        <asp:Button ID="btnUp" Text="上傳圖片" runat="server" OnClick="btnUp_Click" />        <asp:Label id="lblInfo" runat="server" ForeColor="Red" Font-Size="13px" ></asp:Label>        <br />        <br />        <asp:LinkButton ID="lbnPhoto" runat="server" PostBackUrl="~/Album/Photo.aspx" Text="查看照片"></asp:LinkButton>    </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 Album_Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }    private static bool IsAllowedExtension(FileUpload upfile)    {        string strOldFilePath = "";        string strExtension = "";        string[] arrExtension = { ".gif", ".jpg", ".bmp", ".png" };        if (upfile.PostedFile.FileName != string.Empty)        {            strOldFilePath = upfile.PostedFile.FileName;//獲得檔案的完整路徑名             strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));//獲得檔案的副檔名,如:.jpg             for (int i = 0; i < arrExtension.Length; i++)            {                if (strExtension.Equals(arrExtension[i]))                {                    return true;                }            }        }        return false;    }    //圖片上傳並將圖片重新命名    protected void btnUp_Click(object sender, EventArgs e)    {        myClass myclass = new myClass();                 try        {            if (fup.PostedFile.FileName == "")            {                lblInfo.Text = "請選擇檔案!";            }            else            {                //string filepath = fup.PostedFile.FileName;                if (!IsAllowedExtension(fup))                {                    lblInfo.Text = "上傳檔案格式不正確!";                }                if (IsAllowedExtension(fup) == true)                {                    string filepath = fup.PostedFile.FileName;                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);                    string serverpath = Server.MapPath("picture/") + filename;                    fup.PostedFile.SaveAs(serverpath);                    serverpath = "picture/" + filename;                    DateTime now = DateTime.Now;                    string sql = "insert into Photo (photoname,uptime,path)values(‘" + filename + "‘,‘" + now + "‘,‘" + serverpath + "‘)";                    int flag = myclass.DataSQL(sql);                    if (flag == 1)                        lblInfo.Text = "上傳成功!";                    else                        lblInfo.Text = "上傳失敗!";                }                else                {                    lblInfo.Text = "請上傳圖片!";                }            }        }        catch (Exception ex)        {            lblInfo.Text = "上傳發生錯誤!原因是:" + ex.ToString();           }    }}

可以上傳圖片。限制圖片類型。還缺少同時上傳多個檔案和限制圖片大小。最後補充一下看了很久的注釋_(:з」∠)_。發一下馬克一下。說不定魔改完就不見了。。

=L=圖片上傳整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.