C# asp.net实现文件上传

前端代码:

使用visual studio开发实现文件上传

前端页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="scientist.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">    
    var baseText = null; 
    function upOpen() {
        var xzOpen = document.getElementById("xzOpen");
        xzOpen.style.top = "200px"; //窗口距离浏览器内容区最上方的偏移值 
        xzOpen.style.left = "500px"; //窗口距离浏览器内容区最左边的偏移值 
        xzOpen.style.width = "500px"; //窗口的宽度 
        xzOpen.style.height = "300px"; //窗口的高度
        if (baseText == null) baseText = xzOpen.innerHTML;
        xzOpen.innerHTML = baseText + "<div id=\"statusbar\"><button onclick=\"hidePopup(); \">Close window<button></div>"
        var sbar = document.getElementById("statusbar"); 
        sbar.style.marginTop = (parseInt(100)-20) + "px";
        xzOpen.style.visibility = "visible"; 
    document.getElementById("xzOpen").click();
    }
</script>
 <title>上传文档</title>
<style type="text/css"> 
*{ margin:0; padding:0;} 
.exDiv{}
.boxmain{float:left;margin-right:0px;width:100%;} 
.xzOpen{position: absolute; visibility: hidden; overflow: hidden; border:2px solid #CCC; background-color:	#FFCBB3; border:2px solid #333; padding:5px; }
.F1{float:left;margin-top:5px;}
.B1{float:right;margin-top:80px;}
.left{position:absolute;left:0; background:#BBFFBB;height:300px;width:20%} 
.main{margin-right:200px;background:#79FF79; height:300px;;width:100%;margin-left:auto;} 
.up{margin-right:1px;background:#984B4B; height:30px;width:64px;
margin-left:0px;
    }
</style> 
</head>
<body>
    <form id="form1" runat="server">
    <div class="exDiv" style="width:100%; height:80px; margin:0 auto; border:solid 1px #999999;background-color:#95CACA">
    <font >上传文件</font>
    </div>
    <div class="xzOpen" id="xzOpen" >
        <div class="F1" id="F1">
            <asp:FileUpload ID="FileUpload1" runat="server" Width="224px"  />
         </div>
         <div class="B1" id="B1">
            <asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click"  style="margin:0 auto" Width="107px" />
         </div>
    </div>
    </form>
    <div class="boxmain"> 
        <div class="main">main</div> 
    </div> 
    <div class="left"><input type="file" id="xzFile" style="display:none"/> 
         <button type="button" class="up" onclick="upOpen()">选择文件</button>
    </div> 
</body>
</html>

后台C#部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace scientist
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpPostedFile postedFile = this.FileUpload1.PostedFile; //获取到要上传的文件
            String fileName = "";//文件名
            String filePath = "filesNameTest/";//文件保存路径
            fileName = System.IO.Path.GetFileName(postedFile.FileName);//获取文件名称
            if (System.IO.Directory.Exists(Server.MapPath(filePath)) == false)//判断文件夹是否存在
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(filePath));//如果不存在就创建file文件夹
            }
            if (System.IO.File.Exists(Server.MapPath(filePath+fileName)) == true)//判断同名文件是否存在
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert(‘同名文件已存在‘)", true);//弹窗提示文件已存在
            }
            else//文件不存在则保存文件
            {
                if (fileName != "")//判断前端是否有文件传过来
                {
                    String fileSuffix = System.IO.Path.GetExtension(fileName); //获取上传文件的扩展名
                    postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(filePath) + fileName);//保存文件至根目录下的files文件夹里
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert(‘已经保存成功‘)", true);//弹窗提示保存成功
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert(‘请选择文件‘)", true);//弹窗提示未选择文件
                }
            }
        }
    }
}
时间: 2024-08-10 18:47:03

C# asp.net实现文件上传的相关文章

ASP.NET MVC 文件上传和路径处理

ASP.NET MVC 文件上传和路径处理总结 目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传地址的变化 一.上传文件和重复文件处理 文件处理的原则是:不在数据库中保存文件,只在数据库中保存文件信息(Hash值等).采取文件的MD5重命名文件在一般情况足够处理文件的重复问题,强迫症倾向则可以考虑将MD5和其他摘要算法结合. public static string Save(HttpPost

ASP.NET MVC文件上传【转】

最近用到了文件上传功能,下面给出ASP.NET MVC文件上传的一个简单示例: 一.前端代码 @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new {enctype = "multipart/form-data"})) { <div>文件上传:<input type="file" name="myFile"/&g

ASP.NET使用文件上传控件上传图片

ASPX代码 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xht

ASP.NET - 多文件上传,纯代码,不使用插件

解决方案: 前段代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultiFileUpload.aspx.cs" Inherits="WebApplication1.MultiFileUpload1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/x

基于asp.net 的文件上传和下载~~~转

基于asp.net 的文件上传和下载 摘要: 文件的上传和下载是网络中非常重要的两种应用, 本文介绍了使用asp.net 控件实现文件上传的基本方法.解决了当上传大文件时出现各种问题, 并给出了几种解决方案和技巧.另外, 下载文件用二进制读写的方式实现. 1 引言          文件的上传和下载是网络中非常重要的两种应用.其实现方法主要有FTP 方式和HTTP 方式两种, FTP( File Transfer Protocol)是指文件传输协议, 主要用来在网络上传输文件.这种方式虽说文件传

ASP.NET 大文件上传的简单处理

在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的做到多线程的操控和上传进度的显示.在此给大家推荐一款简单易用的上传组件,从而快速便捷得解决了 ASP.NET 中的大文件上传问题. 首先,我们需要这个名为 RanUpLoad 的组件(下面例子中附带),这两个 dll 文件添加到项目的引用中区,xml 文件也要复制在项目中的 bin 文件夹下,也就是

asp.net core文件上传与下载

public class FileController : Controller { /// <summary> /// 跟asp.net webform和asp.net mvc不一样,通过注入的方式,获取项目所在路径 /// </summary> private IHostingEnvironment _hostEnv; public FileController(IHostingEnvironment env) { _hostEnv = env; //其他服务 } public

基于jquery.uploadify的asp.net大文件上传

以前项目的上传都是十几兆的文件,虽然没有进度条,但客户端响应比较快,客户还能接受.在新项目中,客户提供的Excel,要求导入到系统中,但Excel偶尔会将近百兆,客户对上传的使用体验提出了要求.后来在园子里找了朋友写的博客,加上自己的整理,最终实现了基于asp.net的带进度条的百兆文件上传. 在这里首先感谢两位园友,在这里附上参考的两位园友的博客. 马维拉的真实之眼:http://www.cnblogs.com/telephoner/p/3185081.html oec2003:http://

ASP.NET Core 文件上传

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 前言 上篇博文介绍了怎么样在 asp.net core 使用 Redis 和 Protobuf 进行 Session缓存.本篇的是开发过程中使用的一个小功能,怎么做单文件和多文件上传. 如果你觉得对你有帮助的话,不妨点个[推荐]. 目录 单文件