文件上傳到服務器上
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:如何保留原有名稱?
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>

