標籤:配置 soft 檔案上傳 前台 mic nbsp set ext mpi
檔案上傳到伺服器上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>
C#檔案上傳