当前位置:   article > 正文

asp.net表单上传文件

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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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>");
            }
         }
    }
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/515208
推荐阅读
相关标签
  

闽ICP备14008679号