2017-5-27 webform 文件上传

文件上传

准备工作
1.文件上传的页面
2.上传文件要保存的文件夹

1.只要将文件传上来就行
//1、获取要上传的文件,并且知道要上传到服务器的路径
string s = "Uploads/aaa.txt";
//2、生成绝对路径
string path = Server.MapPath(s);
//3、上传
FileUpload1.SaveAs(path);

问题:文件类型保存不住,名字无法修改,覆盖已上传的文件
优化1:保留文件原有的名字
解决:把路径中的文件名替换为控件选中的文件名
string s = "Uploads/" + FileUpload1.FileName;

问题:文件名一样,会覆盖,造成文件丢失
解决:文件名拼接时间和用户名
string s = "Uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + Request.Cookies["user"].Value + FileUpload1.FileName;

问题:文件过大,但是还要上传
解决:不是特别大可以更改请求最大长度,
如果特别大:C#大文件断点续传
扩容:在web.config中的<system.web>标记中
<httpRuntime maxRequestLength="40960"/>

解决:限制上传文件的大小
服务端限制:
if (FileUpload1.PostedFile.ContentLength > 1024 * 1024 * 4)
{
Label1.Text = "文件过大!";
return;
}

客户端限制:
fl.files[0].size > 1024 * 1024 * 4

问题:限制上传文件的类型
<asp:FileUpload ID="FileUpload1" accept=".jpg,.jpeg,.png" runat="server" />

用实例演示一下========================================================

<%@ 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" accept=".jpg,.jpeg,.png" runat="server" />

        <asp:Button ID="Button1" runat="server" Text="保存" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <asp:Image ID="Image1" runat="server" />
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
    document.getElementById("Button1").onclick = function ()
    {
        var fl = document.getElementById("FileUpload1");
        if (fl.value.length <= 0) {
            document.getElementById("Label1").innerHTML = "请先选择要上传的文件";
            return false;
        }
        else
        {
            if (fl.files[0].size > 1024 * 1024 * 4)
            {
                document.getElementById("Label1").innerHTML = "文件过大!不允许超过4MB大小";
                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;
    }

    void Button1_Click(object sender, EventArgs e)

    {
        if( FileUpload1.PostedFile.ContentLength>1024*1024*4 )
        {
            Label1.Text="文件过大!";
            return;
        }
        //获取要上传的文件,并且知道要上传到服务器的路径
        //string s = "Uploads/"+DateTime.Now.ToString("yyyyMMddhhmmssms")+Request.Cookies["user"].Value+FileUpload1.FileName;
        string s = "Uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;
        //上传
        string path = Server.MapPath(s);
        FileUpload1.SaveAs(path);
        Image1.ImageUrl = s;
    }
}
<?xml version="1.0" encoding="utf-8"?>

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

<configuration>

    <system.web>
      <compilation debug="false" targetFramework="4.0" />
      <httpRuntime maxRequestLength="40960"/>
    </system.web>

</configuration>
原文地址:https://www.cnblogs.com/zhengqian/p/6913564.html