0

我有一个从 Site.Master 文件继承布局的网络表单。每次我按下“上传”按钮时,页面都会刷新,但什么也没有发生。我只想让它从“FileUpload”控件上传图像。我在一个没有从 Site.Master 继承布局的页面上使用了这个确切的代码,但我需要它来处理站点。掌握。有任何想法吗?

这是 ASP 部分:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="SignUp.aspx.cs" Inherits="MTCO.SignUp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <asp:TextBox ID="firstNameBox" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:TextBox ID="lastNameBox" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <br />
    <asp:Button ID="uploadButton" runat="server" Text="Upload" />
    <br />
    <br />
    <asp:TextBox ID="statusBox" runat="server">In Progress..</asp:TextBox>
    <br />
</asp:Content>

下面是 C# 部分:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MTCO
{
    public partial class SignUp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void uploadButton_Click(object sender, EventArgs e)
        {
            string filePath = Server.MapPath("~/images/testing/") + FileUpload1.FileName;
            Bitmap photoFile = ResizeImage(FileUpload1.PostedFile.InputStream, 200, 300);
            photoFile.Save(filePath, ImageFormat.Jpeg);
            statusBox.Text = "Uploaded Successfully";
        }

        public Bitmap ResizeImage(Stream stream, int? width, int? height)
        {
            System.Drawing.Bitmap bmpOut = null;
            const int defaultWidth = 800;
            const int defaultHeight = 600;
            int lnWidth = width == null ? defaultWidth : (int)width;
            int lnHeight = height == null ? defaultHeight : (int)height;
            try
            {
                Bitmap loBMP = new Bitmap(stream);
                ImageFormat loFormat = loBMP.RawFormat;

                decimal lnRatio;
                int lnNewWidth = 0;
                int lnNewHeight = 0;

                //*** If the image is smaller than a thumbnail just return it
                if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                {
                    return loBMP;
                }

                if (loBMP.Width > loBMP.Height)
                {
                    lnRatio = (decimal)lnWidth / loBMP.Width;
                    lnNewWidth = lnWidth;
                    decimal lnTemp = loBMP.Height * lnRatio;
                    lnNewHeight = (int)lnTemp;
                }
                else
                {
                    lnRatio = (decimal)lnHeight / loBMP.Height;
                    lnNewHeight = lnHeight;
                    decimal lnTemp = loBMP.Width * lnRatio;
                    lnNewWidth = (int)lnTemp;
                }

                bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                Graphics g = Graphics.FromImage(bmpOut);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
                g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
                loBMP.Dispose();
            }
            catch
            {
                return null;
            }
            return bmpOut;
        }
    }
}
4

2 回答 2

2

您需要将 uploadButton_Click 事件附加到按钮的 OnClick 事件,如下所示:

<asp:Button ID="uploadButton" runat="server" Text="Upload" OnClick="uploadButton_Click" />
于 2012-10-10T06:49:23.710 回答
0

在 VB.net 中,您可以使用如下方法声明:

protected sub uploadButton_Click(object sender, EventArgs e) Handles uploadButton.Click

或按钮标记声明来定义按钮的行为:

<asp:Button ID="uploadButton" runat="server" Text="Upload" OnClick="uploadButton_Click" />

但是,在 C# 中,您只能使用第二种方式。

于 2012-10-10T06:54:41.523 回答