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>");
            }
         }
    }
}
相关推荐
华仔啊37 分钟前
为什么 keySet() 是 HashMap 遍历的雷区?90% 的人踩过
java·后端
9号达人1 小时前
Java 13 新特性详解与实践
java·后端·面试
用户49055816081251 小时前
keepalived原理之持有vip是什么意思
后端
想用offer打牌1 小时前
线程池踩坑之一:将其放在类的成员变量
后端·面试·代码规范
心月狐的流火号1 小时前
Redis 的高性能引擎 Reactor 详解与基于 Go 手写 Redis
redis·后端
橙序员小站1 小时前
搞定系统设计题:如何设计一个支付系统?
java·后端·面试
Java水解1 小时前
Spring Boot + ONNX Runtime模型部署
spring boot·后端
Java水解1 小时前
Spring Security6.3.x使用指南
后端·spring
魂尾ac1 小时前
Django + Vue3 前后端分离技术实现自动化测试平台从零到有系列 <第一章> 之 注册登录实现
后端·python·django·vue