Webform(檔案上傳)

來源:互聯網
上載者:User

標籤:斷點   val   位元組   nec   tac   common   span   src   write   

1.HTML編碼:

<input type="file" />

2.控制項:FileUpload

它是用來選擇要上傳的檔案,還需要一個按鈕來將選中的檔案上傳到伺服器上

string path = "images/" + FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath(path));

最佳化1:檔案保留原有名稱和尾碼

string path = "images/" + FileUpload1.FileName;

最佳化2:防止重名,或是同一時間多個人同時上傳同一名稱檔案

string path = "images/" + DateTime.Now.ToString("yyyyMMddhhssmmms") + Request.Cookies["ures"].Value + FileUpload1.FileName;

最佳化3:可以上傳大檔案,預設是4MB,4096KB
方法:擴容

在Web.config中的system.web標記中添加下面一句:
<httpRuntime maxRequestLength="擴容大小" />

注意:不要擴的太大,因為如果多人同時上傳大檔案,可能會造成伺服器記憶體溢出,導致伺服器崩潰。

最佳化4:超過上傳要求的大小,阻止上傳並提示檔案過大
C#端解決:

if( FileUpload1.PostedFile.ContentLength>(1024*1024*10)){Labei1.Text="檔案超過10M,不要上傳這麼大的!";return;}

C#端不好用,因為如果檔案超過了最大長度,C#端是限制不住的,會直接將程式崩潰

JS端:

document.getElementById("Button1").onclick = function () {        //取出上傳元素        var fi1 = document.getElementById("FileUpload1");        //判斷是否有選中的檔案        if (fi1.value.length <= 0) {            alert(‘請選擇要上傳的檔案!‘);            return false;        }        else {            //驗證選中的檔案長度是否滿足條件            if (fi1.files[0].size > (1024 * 1024 * 10))            {                alert(‘檔案過大,不允許上傳!‘);                return false;            }        }    };

最佳化5:限制可以選擇的檔案類型

在Fileupload屬性裡加 accept=".jpg,.jpeg,.png"

斷點續傳:

 1 using System;  2 using System.Data;  3 using System.Configuration;  4 using System.Collections;  5 using System.Web;  6 using System.Web.Security;  7 using System.Web.UI;  8 using System.Web.UI.WebControls;  9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 using System.IO; 12 public partial class DFile : System.Web.UI.Page 13 { 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 } 17 protected void LinBtnDFile_Click(object sender, EventArgs e) 18 { 19 // 建立一位元數組 20 byte[] buffer = new Byte[10240]; 21 // 指定要下載檔案的路徑. 22 string filePath = @"D:\愛智旮旯.rar"; 23 // 或取檔案名稱包括副檔名 24 string fileName = Path.GetFileName(filePath); 25 Stream fileStream = null; 26 try 27 { 28 // 開啟檔案 29 fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 30 Response.Clear(); 31 // 擷取檔案的大小 32 long fileSize = fileStream.Length; 33 long sum = 0; 34 if (Request.Headers["Range"] != null) 35 { 36 Response.StatusCode = 206; // 表示返回到用戶端的 HTTP 輸出狀態的整數。預設值為 200。 37 sum = long.Parse(Request.Headers["Range"].Replace("bytes=", "").Replace("-", "")); 38 } 39 if (sum != 0) 40 { 41 Response.AddHeader("Content-Range", "bytes " + sum.ToString() + "-" + ((long)(fileSize)).ToString() + "/" + fileSize.ToString()); 42 } 43 // 擷取部分http頭資訊 44 Response.AddHeader("Content-Length", ((long)(fileSize - sum)).ToString()); 45 Response.ContentType = "application/octet-stream"; 46 //擷取檔案來源 47 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(fileName))); 48 // Response.Flush(); 49 fileStream.Position = sum; //設定當前流位置 50 fileSize = fileSize - sum; 51 // 當檔案大小大於0是進入迴圈 52 while (fileSize > 0) 53 { 54 // 判斷用戶端是否仍串連在伺服器 55 if (Response.IsClientConnected) 56 { 57 // 擷取緩衝區中的總位元組數. 58 int length = fileStream.Read(buffer, 0, 10240); 59 // 寫入資料 60 Response.OutputStream.Write(buffer, 0, length); 61 // 將緩衝區的輸出發送到用戶端 62 Response.Flush(); 63 buffer = new Byte[10240]; 64 fileSize = fileSize - length; 65 } 66 else 67 { 68 //當使用者斷開後退出迴圈 69 fileSize = -1; 70 } 71 } 72 } 73 catch (Exception ex) 74 { 75 Response.Write("Error : " + ex.Message); 76 } 77 finally 78 { 79 if (fileStream != null) 80 { 81 //關閉檔案 82 fileStream.Close(); 83 } 84 Response.End(); 85 } 86 } 87 } 

Webform(檔案上傳)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.