麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:29:22
字體:
來源:轉載
供稿:網友

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

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

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

 

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

后臺代碼:

 

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

2 單文件上傳

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

前臺代碼:

 

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

后臺代碼:

 

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

3、多文件上傳

前臺代碼:

 

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

后臺代碼:

 

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

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

 

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

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

 

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

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

改為:

 

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

else if (IsAllowedExtension(FileUpload1) == true)

即變成服務器端檢查上傳文件類型。

5、服務器端檢查上傳文件的類型(文件內部真正的格式)

 

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

后臺代碼:

 

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

是不是內容很精彩,喜歡的朋友就收藏起來吧,以后在遇到ASP.NET文件上傳問題的時候能夠有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 视频一区二区中文字幕 | 国产精品亚洲精品久久 | 护士xxxx| av在线免费播放网站 | 国产无区一区二区三麻豆 | 国产99久久久国产精品下药 | 久久精品在线免费观看 | 成年人高清视频在线观看 | 丰满年轻岳中文字幕一区二区 | 亚洲成在人 | 欧美aⅴ在线观看 | 亚洲小视频在线观看,com | 欧美日韩一区,二区,三区,久久精品 | 国产资源在线观看视频 | 91精品国产九九九久久久亚洲 | 久草在线资源观看 | 天天色综合2 | 在线成人免费网站 | 91久久夜色精品国产网站 | 久久蜜桃精品一区二区三区综合网 | 一级毛片播放 | 美女在线观看视频一区二区 | 毛片免费试看 | 91性高湖久久久久久久久网站 | 毛片一级网站 | 国产精品jk白丝蜜臀av软件 | 国产资源在线看 | 日本最新免费二区三区 | 精品一区二区在线播放 | 婷婷中文字幕一区二区三区 | 羞羞视频2023| a视频在线免费观看 | 日产精品久久久一区二区开放时间 | 黄色成人av在线 | 色视频一区二区 | 中国的免费的视频 | 成人一级免费视频 | 国产激情精品一区二区三区 | 久久亚洲春色中文字幕久久 | 国产精品美女一区二区 | 久久精品操 |