文件上传:
需要使用控件-fileupload
1、如何判断是否选中文件?
FileUpload.FileName -
选中文件的文件名,如果长度不大于0,那么说明没选中任何文件
js - f.value.length
2、如何保存到服务器上?
FileUpload.SaveAs("绝对路径");
3、如何获得绝对路径?
先编写相对路径 - "UpLoads/abc.txt"
将相对路径映射成绝对路径 - Server.MapPath("UpLoads/abc.txt");
4、现在只能上传成为txt文件,并且名字只能是abc
如何保留文件原有的名字和文件类型?
"UpLoads/" + FileUpload1.FileName;
5、如何防止重名覆盖的问题?
"UpLoads/" + Request.Cookies["user"].Value + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;
6、如何限制选中文件的类型?
限制普通人,给控件添加属性 - accept=".jpg,.png,.jpeg,.txt"
限制找事的:
document.getElementById("Button1").onclick = function () {
var fileName = document.getElementById("FileUpload1").value;
var name = fileName.substr(fileName.length - 4, 4);
var name1 = fileName.substr(fileName.length - 5, 5);
if (name != ".jpg" && name != ".png" && name != ".txt" && name1 != ".jpeg")
{
alert("请选择正确的文件!你要找事儿啊??");
return false;
}
};
7、控制上传文件的大小
扩容 - 系统默认允许最大上传长度是4MB
webconfig配置文件中写入
<system.web>
<compilation debug="false" targetFramework="4.0" />
<httpRuntime maxRequestLength="40000" />
</system.web>
注意!不要扩的太多,否则多人同时上传大文件会造成服务器内存不足!
限制大小
C#端限制:
if (FileUpload1.PostedFile.ContentLength>(4*1024*1024))
{ Label1.Text = "文件长度过长!!!";
return; }
JS端限制: var f = document.getElementById("FileUpload1");
if (f.files[0].size > (4 * 1024 * 1024))
{ alert("文件过大!!!");
return false; }
aspx端:两段js代码
<%@ 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,.txt"/> <asp:Button ID="Button1" runat="server" Text="上传" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html> <script type="text/javascript"> document.getElementById("Button1").onclick = function () { var fn = document.getElementById("FileUpload1").value; var name = fn.substr(fn.length - 4, 4); var name1 = fn.substr(fn.length-5,5); if (name != ".jpg" && name != ".png" && name != ".txt" && name1 != ".jpeg") { alert("请选择正确的文件!你要找事儿啊??"); return false; } var f = document.getElementById("FileUpload1"); if (f.files[0].size > (4 * 1024 * 1024)) { alert("文件过大"); return false; } } </script>
webconfig里:用于设定允许上传的文件大小
<configuration> <system.web> <compilation debug="false" targetFramework="4.0" /> <httpRuntime maxRequestLength="40960"/> </system.web> </configuration>
cs里:
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.FileName.Length <= 0) { Label1.Text = "先选中上传文件"; return; } if (FileUpload1.PostedFile.ContentLength > (4 * 1024 * 1024))//判断文件大小 { Label1.Text = "文件过长"; return; } string path = "upload/"+DateTime.Now.ToString("yyyyMMddhhmmssms")+FileUpload1.FileName;//相对路径,加时间防止重名 string epath = Server.MapPath(path);//转换为绝对路径 Label1.Text = epath; FileUpload1.SaveAs(epath); } }
限制文件上传类型:accept
<asp:FileUpload ID="FileUpload1" accept=".jpg,.jpeg,.png" runat="server" />