国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類型)

asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類型)

2019-12-21 13:49lijiao ASP.NET教程

這篇文章主要介紹了asp.net文件上傳解決方案,包括:圖片上傳、單文件上傳、多文件上傳、檢查文件類型等案例,需要的朋友可以參考下

小編之前也介紹了許多ASP.NET文件上傳的解決案例,今天來個asp.net文件上傳大集合。

1 使用標準HTML來進行圖片上傳
前臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<body>
 <form id="form1" runat="server">
 <div>
  <table>
   <tr>
    <td colspan="2" style="height: 21px" >
     使用標準HTML來進行圖片上傳</td>
   </tr>
   <tr>
    <td style="width: 400px">
     <input id="InputFile" style="width: 399px" type="file" runat="server" /></td>
    <td style="width: 80px">
     <asp:Button ID="UploadButton" runat="server" Text="上傳圖片" OnClick="UploadButton_Click" /></td>
   </tr>
   <tr>
    <td colspan="2" >
     <asp:Label ID="Lb_Info" runat="server" ForeColor="Red"></asp:Label></td>    
   </tr>
  </table
 </div>
 </form>
</body>


后臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 
 }
 protected void UploadButton_Click(object sender, EventArgs e)
 {
  string uploadName = InputFile.Value;//獲取待上傳圖片的完整路徑,包括文件名
  //string uploadName = InputFile.PostedFile.FileName;
  string pictureName = "";//上傳后的圖片名,以當前時間為文件名,確保文件名沒有重復
  if (InputFile.Value != "")
  {
   int idx = uploadName.LastIndexOf(".");
   string suffix = uploadName.Substring(idx);//獲得上傳的圖片的后綴名
   pictureName = DateTime.Now.Ticks.ToString() + suffix;
  }
  try
  {
   if (uploadName != "")
   {
    string path = Server.MapPath("~/images/");
    InputFile.PostedFile.SaveAs(path + pictureName);
   }
  }
  catch (Exception ex)
  {
   Response.Write(ex);
  }
 }
}

2 單文件上傳
這是最基本的文件上傳,在asp.net1.x中沒有這個FileUpload控件,只有html的上傳控件,那時候要把html控件轉化為服務器控件, 很不好用。其實所有文件上傳的美麗效果都是從這個FileUpload控件衍生,第一個例子雖然簡單卻是根本。
前臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<body>
 <form id="form1" runat="server">
 <div>
  <table style="width: 90%">
   <tr>
    <td style="width: 159px" colspan=2>
     <strong><span style="font-size: 10pt">最簡單的單文件上傳</span></strong></td>
   </tr>
   <tr>
    <td style="width: 600px">
     <asp:FileUpload ID="FileUpload1" runat="server" Width="600px" /></td>
    <td align=left>
     <asp:Button ID="FileUpload_Button" runat="server" Text="上傳圖片" OnClick="FileUpload_Button_Click" /></td>
   </tr>
   <tr>
    <td colspan=2>
     <asp:Label ID="Upload_info" runat="server" ForeColor="Red" Width="767px"></asp:Label></td>
   </tr>
  </table
 </div>
 </form>
</body>

后臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 
 }
 protected void FileUpload_Button_Click(object sender, EventArgs e)
 {
  try
  {
   if (FileUpload1.PostedFile.FileName == "")
   //if (FileUpload1.FileName == "")
   //if (!FileUpload1.HasFile)  //獲取一個值,該值指示 System.Web.UI.WebControls.FileUpload 控件是否包含文件。包含文件,則為 true;否則為 false。
   {
    this.Upload_info.Text = "請選擇上傳文件!";
   }
   else
   {
    string filepath = FileUpload1.PostedFile.FileName; //得到的是文件的完整路徑,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg
    //string filepath = FileUpload1.FileName;    //得到上傳的文件名20022775_m.jpg
    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg
    string serverpath = Server.MapPath("~/images/") + filename;//取得文件在服務器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg
    FileUpload1.PostedFile.SaveAs(serverpath);//將上傳的文件另存為
    this.Upload_info.Text = "上傳成功!";
   }
  }
  catch (Exception ex)
  {
   this.Upload_info.Text = "上傳發生錯誤!原因是:" + ex.ToString();
  }
 }
}


3、多文件上傳
前臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<body>
 <form id="form1" runat="server">
 <div>
 <table style="width: 343px">
   <tr>
    <td style="width: 100px">
     多文件上傳</td>
    <td style="width: 100px">
    </td>
   </tr>
   <tr>
    <td style="width: 100px">
     <asp:FileUpload ID="FileUpload1" runat="server" Width="475px" />
     </td>
    <td style="width: 100px">
     </td>
   </tr>
   <tr>
    <td style="width: 100px">
     <asp:FileUpload ID="FileUpload2" runat="server" Width="475px" /></td>
    <td style="width: 100px">
    </td>
   </tr>
   <tr>
    <td style="width: 100px">
     <asp:FileUpload ID="FileUpload3" runat="server" Width="475px" /></td>
    <td style="width: 100px">
    </td>
   </tr>
   <tr>
    <td style="width: 100px">
     <asp:Button ID="bt_upload" runat="server" OnClick="bt_upload_Click" Text="一起上傳" />
     <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="448px"></asp:Label></td>
    <td style="width: 100px">
    </td>
   </tr>
  </table>
 </div>
 </form>
</body>


后臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 
 }
 protected void bt_upload_Click(object sender, EventArgs e)
 {
  if (FileUpload1.PostedFile.FileName == "" && FileUpload2.PostedFile.FileName == "" && FileUpload3.PostedFile.FileName == "")
  {
   this.lb_info.Text = "請選擇文件!";
  }
  else
  {
   HttpFileCollection myfiles = Request.Files;
   for (int i = 0; i < myfiles.Count; i++)
   {
    HttpPostedFile mypost = myfiles[i];
    try
    {
     if (mypost.ContentLength > 0)
     {
      string filepath = mypost.FileName;//C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg
      string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg
      string serverpath = Server.MapPath("~/images/") + filename;//C:\Inetpub\wwwroot\WebSite2\images\20022775_m.jpg
      mypost.SaveAs(serverpath);
      this.lb_info.Text = "上傳成功!";
     }
    }
    catch (Exception ex)
    {
     this.lb_info.Text = "上傳發生錯誤!原因:" + ex.Message.ToString();
    }
   }
  }
 }
}

4、客戶端檢查上傳文件類型(以上傳圖片為例)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
 <title>客戶端檢查上傳文件類型</title>
 <script language="javascript">
 function Check_FileType()
 {
  var str=document.getElementById("FileUpload1").value;
  var pos=str.lastIndexOf(".");
  var lastname=str.substring(pos,str.length);
  if(lastname.toLowerCase()!=".jpg"&&lastname.toLowerCase()!=".gif")
  {
   alert("您上傳的文件類型為"+lastname+",圖片必須為.jpg,.gif類型");
   return false;
  }
  else
  {
   return true;
  }  
 }
 </script>
</head>
<body>
 <form id="form1" runat="server">
 <div>
  <table>
   <tr>
    <td colspan="2">
     客戶端檢查上傳文件類型</td>    
   </tr>
   <tr>
    <td style="width: 444px">
     <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td>
    <td style="width: 80px">
     <asp:Button ID="bt_upload" runat="server" Text="上傳圖片" OnClick="bt_upload_Click" OnClientClick="return Check_FileType()" /></td>
   </tr>
   <tr>
    <td colspan="2" style="height: 21px">
     <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></asp:Label></td>    
   </tr>
  </table
 </div>
 </form>
</body>
</html>

注意:點擊上傳時先觸發客戶端事件OnClientClick="return Check_FileType()"

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 
 }
 
 protected void bt_upload_Click(object sender, EventArgs e)
 {
  try
  {
   if (FileUpload1.PostedFile.FileName == "")
   {
    this.lb_info.Text = "請選擇文件!";
   }
   else
   {
    string filepath = FileUpload1.PostedFile.FileName;
    //if (!IsAllowedExtension(FileUpload1))
    //{
    // this.lb_info.Text = "上傳文件格式不正確!";
    //}
    if (IsAllowedExtension(FileUpload1) == true)
    {
     string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
     string serverpath = Server.MapPath("~/images/") + filename;
     FileUpload1.PostedFile.SaveAs(serverpath);
     this.lb_info.Text = "上傳成功!";
    }
    else
    {
     this.lb_info.Text = "請上傳圖片!";
    }
   }
  }
  catch (Exception ex)
  {
   this.lb_info.Text = "上傳發生錯誤!原因:" + ex.ToString();
  }
 }
 private static bool IsAllowedExtension(FileUpload upfile)
 {
  string strOldFilePath = "";
  string strExtension="";
  string[] arrExtension ={ ".gif", ".jpg", ".bmp", ".png" };
  if (upfile.PostedFile.FileName != string.Empty)
  {
   strOldFilePath = upfile.PostedFile.FileName;//獲得文件的完整路徑名
   strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));//獲得文件的擴展名,如:.jpg
   for (int i = 0; i < arrExtension.Length; i++)
   {
    if (strExtension.Equals(arrExtension[i]))
    {
     return true;
    }
   }
  }
  return false;
 }
}

注意:若去掉客戶端的腳本和客戶端事件OnClientClick="return Check_FileType()",在后臺代碼
改為:

?
1
2
3
4
if (!IsAllowedExtension(FileUpload1))
    {
     this.lb_info.Text = "上傳文件格式不正確!";
    }


else if (IsAllowedExtension(FileUpload1) == true)
即變成服務器端檢查上傳文件類型。
5、服務器端檢查上傳文件的類型(文件內部真正的格式)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<body>
 <form id="form1" runat="server">
 <div>
  <table>
   <tr>
    <td colspan="2">
     服務器檢查上傳文件類型</td>    
   </tr>
   <tr>
    <td style="width: 444px">
     <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td>
    <td style="width: 80px">
     <asp:Button ID="bt_upload" runat="server" Text="上傳圖片" OnClick="bt_upload_Click" /></td>
   </tr>
   <tr>
    <td colspan="2" style="height: 21px">
     <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></asp:Label></td>    
   </tr>
  </table
 </div>
 </form>
</body>

后臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
 
public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 
 }
 protected void bt_upload_Click(object sender, EventArgs e)
 {
  try
  {
   if (FileUpload1.PostedFile.FileName == "")
   {
    this.lb_info.Text = "請選擇文件!";
   }
   else
   {
    string filepath = FileUpload1.PostedFile.FileName;
    if (IsAllowedExtension(FileUpload1) == true)
    {
     string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
     string serverpath = Server.MapPath("images/") + filename;
     FileUpload1.PostedFile.SaveAs(serverpath);
     this.lb_info.Text = "上傳成功!";
    }
    else
    {
     this.lb_info.Text = "請上傳圖片";
    }
   }
  }
  catch (Exception error)
  {
   this.lb_info.Text = "上傳發生錯誤!原因:" + error.ToString();
  }
 }
 private static bool IsAllowedExtension(FileUpload upfile)
 {
  FileStream fs = new FileStream(upfile.PostedFile.FileName, FileMode.Open, FileAccess.Read);
  BinaryReader r = new BinaryReader(fs);
  string fileclass = "";
  byte buffer;
  try
  {
   buffer = r.ReadByte();
   fileclass = buffer.ToString();
   buffer = r.ReadByte();
   fileclass += buffer.ToString();
  }
  catch
  {
    
  }
  r.Close();
  fs.Close();
  if (fileclass == "255216" || fileclass == "7173"||fileclass=="6677"||fileclass=="13780")//說明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
  {
   return true;
  }
  else
  {
   return false;
  }
 }
}
是不是內容很精彩,喜歡的朋友就收藏起來吧,以后在遇到ASP.NET文件上傳問題的時候能夠有所幫助。

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩一 | 中文字幕亚洲一区二区三区 | 中文字幕在线观看一区 | 一级黄色大片在线 | 久久精品成人 | 国产视频黄在线观看 | 91视频国产网站 | 日本一区二区在线免费 | 亚洲一区 中文字幕 | 亚洲视频在线观看 | 亚洲欧美另类在线 | 中文字幕在线一区 | 在线免费看黄视频 | 亚洲欧美日韩精品久久亚洲区 | 日韩免费 | 成人一区二区在线观看 | 97久久久 | 亚洲综合精品 | 国产精品一区二区三 | 一区二区三区免费在线观看 | 亚洲午夜精品 | 日韩在线视频一区 | 亚洲国产精品自拍视频 | 日韩在线一区二区三区 | 国精品一区二区三区 | 91精品国产一区二区 | 国产美女视频网站 | 亚洲人成在线播放 | 福利片在线免费观看 | 国产成人精品av | 手机在线观看av | 午夜欧美精品久久久久 | 国产一区二区在线播放 | 成人小视频在线看 | 日韩精品二区 | 国产亚洲片 | 亚洲特黄一级 | 久久精品一区二区三区四区 | 黄毛片免费看 | 国产午夜精品福利 | 九一视频在线观看 |