C#檔案上傳

來源:互聯網
上載者:User

標籤:配置   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#檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.