asp.net表单上传文件

asp.net表单上传文件

可以用自己的主机搭建服务器环境测试

1.借鉴文章:

原文链接:http://www.cnblogs.com/gxwang/p/4883902.html

2.html端示例代码,

文件名为index.aspx

复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="upload_file_lianxi.index" %>

<!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>

            <p>服务器端控件上传</p>
            <asp:FileUpload ID="MyFileUpload" runat="server" /> 
            <asp:Button ID="FileUploadButton" runat="server" Text="上传" 
                    onclick="FileUploadButton_Click" />
        </div>
    </form>
</body>
</html>

3.index.aspx.cs示例代码

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }
        protected void FileUploadButton_Click(object sender, EventArgs e)
          {
            
            if (MyFileUpload.HasFile)
              {
                // string filePath = Server.MapPath("~/UploadFiles/");//原创,此处必须事先建立文件夹UploadFiles,否则报错
                //string filePath = Server.MapPath("~/");//经测试效果同下面的,其实就是网站发布的根目录
                string filePath = Server.MapPath("/");//其实就是网站发布的根目录
                  string fileName = MyFileUpload.PostedFile.FileName;
                  MyFileUpload.SaveAs(filePath + fileName);
                  //Response.Write("<p >上传成功!</p>");
                  Response.Write(filePath);
              }
            else
             {
                 Response.Write("<p >请选择要上传的文件!</p>");
            }
         }
    }
}
相关推荐
nbwenren5 小时前
Springboot中SLF4J详解
java·spring boot·后端
helx825 小时前
SpringBoot中自定义Starter
java·spring boot·后端
rleS IONS6 小时前
SpringBoot获取bean的几种方式
java·spring boot·后端
lifewange6 小时前
Go语言-开源编程语言
开发语言·后端·golang
白毛大侠7 小时前
深入理解 Go:用户态和内核态
开发语言·后端·golang
王码码20357 小时前
Go语言中的数据库操作:从sqlx到ORM
后端·golang·go·接口
星辰_mya8 小时前
雪花算法和时区的关系
数据库·后端·面试·架构师
计算机学姐9 小时前
基于SpringBoot的兴趣家教平台系统
java·spring boot·后端·spring·信息可视化·tomcat·intellij-idea
總鑽風9 小时前
单点登录springcloud+mysql
后端·spring·spring cloud
0xDevNull9 小时前
Java 11 新特性概览与实战教程
java·开发语言·后端