尝试上传图片时,按下按钮,页面刷新,图片无法上传。到底是怎么回事?

时间:2012-10-10 06:45:41

标签: c# asp.net

我有一个从Site.Master文件继承布局的webform。每次按“上传”按钮,页面都会刷新,没有任何反应。我只是想让它从“FileUpload”控件上传图像。我在一个不从Site.Master继承布局的页面上使用了这个确切的代码,但是我需要这个来处理Site。主。有什么想法吗?

这是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#Part:

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;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

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

或用于定义buttom行为的按钮标记声明:

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

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