文件上传

上传:Fileupload控件 只是选择 需要来一个按钮提交

前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
        <asp:FileUpload ID="FileUpload1" runat="server"accept=".jpg,.png" /><%--accept=".jpg,.png" 只能上传jpg和png格式--%>
        <asp:Button ID="Button1" runat="server" Text="上传" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>
<script>
    document.getElementById("Button1").onclick = function () {
        var file1 = document.getElementById("FileUpload1");
        if (file1.files[0].size>(2*1024*1024))   //获取文件的大小file1.files[0].size
        {
            document.getElementById("Label1").innerHTML = "JS告诉你文件过大";
            return false;
        }
    }

</script>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;//上传按钮
    }
    //上传按钮点击事件
    private void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.postedFile.ContentLenth>(1024*1024*2))//文件小于2M才给上传如果文件超过了显示的大小还是会报错
        {
            提示过大
        }
        else { }
        string path = "uploads/" + FileUpload1.FileName;  //获取新建文件夹uploads的路径  +FileUpload1.FileName原文件的名字
        string path1 = "uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;//防止传的文件重名加个时间精确到毫秒
        string path2 = "uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + Request.Cookies["user"].Value + FileUpload1.FileName;//防止传的文件重名加个时间精确到毫秒再加用户名
        string endpath = Server.MapPath(path);//设置绝对路径 (要获取绝对路径的文件)
        FileUpload1.SaveAs(endpath);//将上传的内容保存到获取的绝对路径  需要一个路径 建一个文件夹uploads放保存的东西   ()括号内为绝对路径

    }
}
}

Web.config  代码

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.0" />
      <httpRuntime maxRequestLength="40960"/><!--设置最大上传40MB 以b为单位-->
    </system.web>

</configuration>

文件上传 1、如何把文件传到服务器上?

问题1:如何保留原有名称?

 string path = "uploads/" + FileUpload1.FileName; 

问题2:重名问题如何解决? ;

  string path2 = "uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + Request.Cookies["user"].Value + FileUpload1.FileName;//防止传的文件重名加个时间精确到毫秒再加用户名

问题3:如何限制选择文件的类型?

  前台代码

  <asp:FileUpload ID="FileUpload1" runat="server"accept=".jpg,.png" /><%--accept=".jpg,.png" 只能上传jpg和png格式--%>

问题4:大文件问题 程序默认允许的上传文件大小为 4MB

一、扩容   C#大文件上传/断点续传

  Web.config

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.0" />
      <httpRuntime maxRequestLength="40960"/><!--设置最大上传40MB 以b为单位-->
    </system.web>

</configuration>

 二、限制  服务端限制   

   

 //上传按钮点击事件
    private void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.postedFile.ContentLenth>(1024*1024*2))//文件小于2M才给上传如果文件超过了显示的大小还是会报错
        {
            提示过大
        }
        else { }
        string path = "uploads/" + FileUpload1.FileName;  //获取新建文件夹uploads的路径  +FileUpload1.FileName原文件的名字
        string path1 = "uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;//防止传的文件重名加个时间精确到毫秒
        string path2 = "uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + Request.Cookies["user"].Value + FileUpload1.FileName;//防止传的文件重名加个时间精确到毫秒再加用户名
        string endpath = Server.MapPath(path);//设置绝对路径 (要获取绝对路径的文件)
        FileUpload1.SaveAs(endpath);//将上传的内容保存到获取的绝对路径  需要一个路径 建一个文件夹uploads放保存的东西   ()括号内为绝对路径

    }

JS客户端限制

<script>
    document.getElementById("Button1").onclick = function () {
        var file1 = document.getElementById("FileUpload1");
        if (file1.files[0].size>(2*1024*1024))   //获取文件的大小file1.files[0].size
        {
            document.getElementById("Label1").innerHTML = "JS告诉你文件过大";
            return false;
        }
    }

</script>
原文地址:https://www.cnblogs.com/skyhorseyk/p/7364465.html