沉迷摸鱼无法自拔 2023-10-03 23:30 采纳率: 70.6%
浏览 10
已结题

请问这个问题如何解决(关键词-File)

上传图片后网页无法访问
源代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication11.WebForm1" %>

<!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="Button2" runat="server" OnClick="Button2_Click" Text="文件上传" />
        </div>
        <asp:Image ID="Image1" runat="server" Height="125px" Width="161px" />
        <p>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </p>
    </form>
</body>
</html>



程序控件

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            bool filesValid = 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])
                    {
                        filesValid = true;
                    }
                    if(filesValid==true)
                    {
                        try
                        {
                            this.Image1.ImageUrl = "-/images/" + FileUpload1.FileName;
                            this.FileUpload1.SaveAs(Server.MapPath("`/images/") + FileUpload1.FileName);
                            this.Label1.Text = "文件上传成功";
                            this.Label1.Text += "<Br/>";
                            this.Label1.Text += "<li>" + "文件路径:" + this.FileUpload1.PostedFile.FileName;
                            this.Label1.Text += "<Br/>";
                            this.Label1.Text += "<li>" + "文件大小:" + this.FileUpload1.PostedFile;
                            this.Label1.Text += "<Br/>";
                            this.Label1.Text += "<li>" + "文件类型:" + this.FileUpload1.PostedFile.ContentType;
                        }
                        catch
                        {
                            this.Label1.Text = "无法上传文件";
                        }
                    }
                }
            }
        }
    }
}

  • 写回答

17条回答 默认 最新

  • threenewbee 2023-10-03 23:35
    关注
    获得0.75元问题酬金

    页面autopostback设置为true
    this.Image1.ImageUrl = "-/images/" + FileUpload1.FileName;
    这里看看url是什么
    然后上传以后,在浏览器里看看前端img的src指向了什么

    评论

报告相同问题?

问题事件

  • 系统已结题 10月11日
  • 创建了问题 10月3日

悬赏问题

  • ¥20 校园二手交易小程序搭建
  • ¥15 请问在ubuntu用conda创建环境报错怎么能解决
  • ¥15 STM32CubeMX/proteus按键控制指示灯颜色切换
  • ¥20 python,计算区位熵和扩张指数
  • ¥15 Python环境配置
  • ¥15 大四学生的困惑,有偿提问!
  • ¥15 解决页面无法编入索引:被“noindex”标签排除的问题?
  • ¥15 arduino测量电阻
  • ¥15 快手uid转快手号谁能解决 需要开发
  • ¥15 iis部署Django时css不生效,来个真人,ai不好使