标签:配置 soft 文件上传 前台 mic nbsp set ext mpi
<%@ 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放保存的东西 ()括号内为绝对路径
}
}
}
<?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>
string path = "uploads/" + FileUpload1.FileName;
string path2 = "uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + Request.Cookies["user"].Value + FileUpload1.FileName;//防止传的文件重名加个时间精确到毫秒再加用户名
<asp:FileUpload ID="FileUpload1" runat="server"accept=".jpg,.png" /><%--accept=".jpg,.png" 只能上传jpg和png格式--%>
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放保存的东西 ()括号内为绝对路径
}
<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>
标签:配置 soft 文件上传 前台 mic nbsp set ext mpi
原文地址:http://www.cnblogs.com/wcl2017/p/7639450.html