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>");
            }
         }
    }
}
相关推荐
程序员侠客行30 分钟前
Mybatis连接池实现及池化模式
java·后端·架构·mybatis
Honmaple36 分钟前
QMD (Quarto Markdown) 搭建与使用指南
后端
PP东1 小时前
Flowable学习(二)——Flowable概念学习
java·后端·学习·flowable
invicinble1 小时前
springboot的核心实现机制原理
java·spring boot·后端
全栈老石1 小时前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
space62123272 小时前
在SpringBoot项目中集成MongoDB
spring boot·后端·mongodb
Tony Bai2 小时前
再见,丑陋的 container/heap!Go 泛型堆 heap/v2 提案解析
开发语言·后端·golang
寻找奶酪的mouse3 小时前
30岁技术人对职业和生活的思考
前端·后端·年终总结
梦想很大很大3 小时前
使用 Go + Gin + Fx 构建工程化后端服务模板(gin-app 实践)
前端·后端·go
毅炼3 小时前
Java 基础常见问题总结(4)
java·后端