日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

ASP.NET实现文件上传

發(fā)布時(shí)間:2025/3/20 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET实现文件上传 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

新建ASP.NET Web應(yīng)用程序

單文件上傳

效果

頁(yè)面代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebDropzone.WebForm" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"> <head><meta name="viewport" content="width=device-width" /><title></title> </head> <body><form id="form1" runat="server"><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"><asp:Button ID="bt_upload" runat="server" OnClick="bt_upload_Click" Text="上傳" /></td></tr><tr><td style="width: 100px; height: 21px;"><asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="183px"></asp:Label></td><td style="width: 100px; height: 21px"></td></tr></table></form> </body> </html>

后臺(tái)代碼

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc;namespace WebDropzone {public partial class WebForm : System.Web.UI.Page, IHttpHandler{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 = "請(qǐng)選擇文件!";}else{string filepath = FileUpload1.PostedFile.FileName;string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);string serverpath = Server.MapPath("File/") + filename;FileUpload1.PostedFile.SaveAs(serverpath);this.lb_info.Text = "上傳成功!";}}catch (Exception ex){this.lb_info.Text = "上傳發(fā)生錯(cuò)誤!原因是:" + ex.ToString();}}} }

多文件上傳

效果

頁(yè)面代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebDropzone.WebForm" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"><meta name="viewport" content="width=device-width" /><title></title> </head> <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="183px"></asp:Label></td><td style="width: 100px"></td></tr></table></div></form> </body> </html>

后臺(tái)代碼

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc;namespace WebDropzone {public partial class WebForm : System.Web.UI.Page, IHttpHandler{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 = "請(qǐng)選擇文件!";}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;string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);string serverpath = Server.MapPath("File/") + filename;mypost.SaveAs(serverpath);this.lb_info.Text = "上傳成功!";}}catch (Exception error){this.lb_info.Text = "上傳發(fā)生錯(cuò)誤!原因:" + error.ToString();}}}}} }

在實(shí)際應(yīng)用中,這種方式是不可行的,因?yàn)椴豢赡茏層脩敉ㄟ^(guò)多次點(diǎn)擊多個(gè)按鈕,進(jìn)行多個(gè)文件的上傳,而是可以直接選擇多個(gè)文件,直接實(shí)現(xiàn)上傳,其次,界面太丑,單純的通過(guò)ASP.ENT服務(wù)器控件實(shí)行文件上傳功能很少使用。

總結(jié)

以上是生活随笔為你收集整理的ASP.NET实现文件上传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。