国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

擁有網(wǎng)頁(yè)版小U盤(pán) ASP.NET實(shí)現(xiàn)文件上傳與下載功能

2024-07-10 12:46:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

今天看到了一篇不錯(cuò)的文章,就拿來(lái)一起分享一下吧。
實(shí)現(xiàn)的是文件的上傳與下載功能。

關(guān)于文件上傳:
談及文件上傳到網(wǎng)站上,首先我們想到的就是通過(guò)什么上傳呢?在ASP.NET中,只需要用FileUpload控件即可完成,但是默認(rèn)上傳4M大小的數(shù)據(jù),當(dāng)然了你可以在web.config文件中進(jìn)行修改,方式如下:

<system.web>  <httpRuntime executionTimeout="240"    maxRequestLength="20480"/></system.web>

但是這種方式雖然可以自定義文件的大小,但并不是無(wú)極限的修改的

下一步,現(xiàn)在“工具”有了,要怎么上傳呢?按照直覺(jué)是不是應(yīng)該先選中我想要上傳的文件呢?這就對(duì)了,因?yàn)閺腇ileUpload控件返回后我們便已經(jīng)得到了在客戶端選中的文件的信息了,接下來(lái)就是將這個(gè)文件進(jìn)行修改(具體的操作是:去掉所得路徑下的盤(pán)符的信息,換成服務(wù)器上的相關(guān)路徑下,不過(guò)這里并沒(méi)有更改原本文件的名稱)。然后調(diào)用相關(guān)的上傳方法就好了。

先看一下界面文件吧

<form id="form1" runat="server">    <asp:FileUpload ID="FileUpload1" runat="server" />    <br />    <br />    <br />    <br />    <br />    <br />    <asp:ImageButton ID="ImageButton_Up" runat="server" OnClick="ImageButton_Up_Click" style="text-decoration: underline" ToolTip="Up" Width="54px" />           <asp:ImageButton ID="ImageButton_Down" runat="server" OnClick="ImageButton_Down_Click" ToolTip="Download" Width="51px" />    <br />    <br />         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>   </form>

然后是具體的邏輯

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)  {  }  //a method for currying file updown  private void UpFile()  {    String strFileName;    //get the path of the file    String FilePath = Server.MapPath("./") + "File";    //judge weather has file to upload    if (FileUpload1.PostedFile.FileName != null)    {      strFileName = FileUpload1.PostedFile.FileName;      //save all the message of the file      strFileName = strFileName.Substring(strFileName.LastIndexOf("http://") + 1);      try      {        FileUpload1.SaveAs(FilePath + "http://" + this.FileUpload1.FileName);        //save the file and obey the rules        Label1.Text = "Upload success!";      }      catch (Exception e)      {        Label1.Text = "Upload Failed!"+e.Message.ToString();      }    }  }  protected void ImageButton_Up_Click(object sender, ImageClickEventArgs e)  {    UpFile();  }  protected void ImageButton_Down_Click(object sender, ImageClickEventArgs e)  {    Response.Redirect("DownFile.aspx");  }}            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三明市| 察隅县| 称多县| 赤水市| 手机| 平原县| 松江区| 姚安县| 梨树县| 龙州县| 永康市| 汶上县| 丹巴县| 介休市| 突泉县| 瓦房店市| 潮州市| 马龙县| 商丘市| 土默特右旗| 高平市| 磴口县| 松潘县| 镇沅| 平安县| 洪湖市| 昭苏县| 铁力市| 沾化县| 盐源县| 林口县| 铁岭市| 武宁县| 玉溪市| 涡阳县| 靖边县| 宁化县| 彰化市| 江津市| 辽宁省| 青浦区|