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>");
            }
         }
    }
}
相关推荐
IT_陈寒20 分钟前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
流浪克拉玛依1 小时前
Go Web 服务限流器实战:从原理到压测验证 --使用 Gin 框架 + Uber Ratelimit / 官方限流器,并通过 Vegeta 进行性能剖析
后端
孟沐1 小时前
保姆级教程:手写三层架构 vs MyBatis-Plus
后端
星浩AI1 小时前
让模型自己写 Skills——从素材到自动生成工作流
人工智能·后端·agent
华仔啊3 小时前
为啥不用 MP 的 saveOrUpdateBatch?MySQL 一条 SQL 批量增改才是最优解
java·后端
武子康4 小时前
大数据-242 离线数仓 - DataX 实战:MySQL 全量/增量导入 HDFS + Hive 分区(离线数仓 ODS
大数据·后端·apache hive
砍材农夫5 小时前
TCP和UDP区别
后端
千寻girling5 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
千寻girling5 小时前
Python 是用来做 AI 人工智能 的 , 不适合开发 Web 网站 | 《Web框架》
人工智能·后端·算法
贾铭5 小时前
如何实现一个网页版的剪映(三)使用fabric.js绘制时间轴
前端·后端