ASP.NET 文件上傳于下載

  本文主要介紹一下,在APS.NET中文件的簡單上傳于下載,上傳是將文件上傳到服務器的指定目錄下,下載是從存入數據庫中的路徑,從服務器上下載。

  1.上傳文件

  (1)頁面代碼

   <table align="center" cellpadding="0" cellspacing="0" width="100%"><tr><td align="right">文件名稱:</td><td><asp:TextBox ID="FileName" runat="server"></asp:TextBox></td><td></td></tr>  <tr><td align="right">瀏覽:</td><td><asp:FileUpload ID="FileUpload1" runat="server" /></td><td></td></tr>   <tr><td></td><td><asp:Button ID="UploadButton" runat="server" Text="上傳" onclick="UploadButton_Click" /><asp:Label ID="Msg" runat="server" ForeColor="#FF0000"></asp:Label>     </td><td></td></tr>               </table>

  (2).后臺代碼  

protected void UploadButton_Click(object sender, EventArgs e){Msg.Text = "";MyWebSite.DAL.FileInfo file = new MyWebSite.DAL.FileInfo();if (string.IsNullOrEmpty(FileName.Text)){Msg.Text = "請輸入文件名";return;}if (FileUpload1.HasFile){try{DateTime upload = DateTime.Now;string flag = Path.GetExtension(FileUpload1.PostedFile.FileName);string path = Server.MapPath("../Upload/" + FileUpload1.FileName);string size = (FileUpload1.PostedFile.ContentLength / 1024).ToString();FileUpload1.SaveAs(path);file.FileName = FileName.Text.ToString();file.Type = flag;//獲得文件格式file.Size = size;//文件大小file.FilePath = path;//上傳到服務器的絕對路徑file.UploadTime = DateTime.Now.ToShortDateString();//上傳時間handler.AddFile(file);//將上傳的文件信息保存到數據庫Msg.Text = "上傳成功!";}catch{Msg.Text = "上傳失敗!";}}}

  2.下載文件

  (1)前臺頁面

<asp:Repeater ID="Repeater1" runat="server" onitemcommand="down_file_Click"><ItemTemplate>       <table width="90%" align="center" border="1" cellpadding="1" cellspacing="0" bgcolor="#e1e1e1" class="title_font"><tr><td class="title_font" width="10%"  align="center">文件名稱:</td><td width="20%"align="center"><b><asp:Label ID="FileTitle" runat="server" Text='<%#Eval("文件名稱") %>'></asp:Label></b></td><td  width="6%"align="center">類型:</td><td width="6%"align="center"><%#Eval("類型") %></td>                <td width="10%" align="center">文件大小:</td><td  width="8%"align="center"><%#Eval("文件大小") %>KB</td><td width="10%"align="center">上傳時間:</td><td ><%#Eval("上傳時間") %></td><td  width="10%" colspan="2" align="center"><asp:LinkButton ID="LinkButton1" CommandArgument='<%#Eval("link") %>' runat="server">下載文件</asp:LinkButton></td></tr>           </table><br /></ItemTemplate>
</asp:Repeater>

  (2)后臺代碼

  1).綁定數據

        public void GrvDataBin(List<FileInfo> list){DataView dv = new DataView();DataTable dt = new DataTable("fileMeta");{dt.Columns.Add("文件名稱");dt.Columns.Add("類型");dt.Columns.Add("上傳時間");dt.Columns.Add("文件大小");dt.Columns.Add("link");}foreach (FileInfo fileM in list){DataRow row = dt.NewRow();row[0] = fileM.FileName;row[1] = fileM.Type;row[2] = fileM.UploadTime;row[3] = fileM.Size;row[4] = fileM.FilePath;dt.Rows.Add(row);}dv.Table = dt;Repeater1.DataSource = dv;Repeater1.DataBind();}

  2).下載文件

  

protected void down_file_Click(object sender, RepeaterCommandEventArgs e){System.IO.FileStream fs = null;try{string filePath = e.CommandArgument.ToString();string fileName = ((Label)e.Item.FindControl("FileTitle")).Text.ToString();fs = System.IO.File.OpenRead(filePath);byte[] buffer = new byte[1024];long count = 1024;Response.Buffer = true;Response.AddHeader("Connection", "Keep-Alive");Response.ContentType = "application/octet-stream";Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(System.IO.Path.GetFileName(filePath)));//下載時要保存的默認文件名Response.AddHeader("Content-Length", fs.Length.ToString());while (count == 1024){count = fs.Read(buffer, 0, 1024);Response.BinaryWrite(buffer);}}catch (Exception ex){return;}finally{fs.Close();}}

  以上就完成了,簡單的文件上傳與下載。 

?

?

轉載于:https://www.cnblogs.com/akwwl/p/3184239.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274164.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274164.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274164.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

idea重要插件代碼顏色_顏色在您的網站上的重要性和品牌形象

idea重要插件代碼顏色Choosing the right colors for a website or a logo can be a perplexing and time-consuming task, unless you have the right knowledge of colors. Colors play a pivotal role in the success of some businesses and can make a huge impact on the…

【自己給自己題目做】之一:橢圓可點擊區域

【題一】請實現以下需求&#xff0c;要做一個活動頁面&#xff0c;頁面上有一張圖片&#xff08;假設是800x600&#xff09;&#xff0c;圖片正中心有一個橢圓形的可點擊區域&#xff0c;假設橢圓長軸為200px&#xff08;橫向&#xff09;&#xff0c;短軸160px&#xff08;縱向…

軟件設計師中級 百度知道_設計師應該知道什么

軟件設計師中級 百度知道… and no, it doesn’t have to be how to code.……而且&#xff0c;不必一定要編碼。 Here are a few points that have helped me assess what technical knowledge is necessary for designers to be successful.以下幾點幫助我評估了設計師成功所…

看看清華的同學在四年的大學中干什么吧,非常值得學習

&#xff08;一&#xff09; 永遠不要說你已經盡力了 我在高中時體育特別差&#xff0c;跑1000米都很要命&#xff0c;從來都是不及格。到了清華之后&#xff0c;第一節體育課&#xff0c;老師告訴我們每年要測3000米長跑&#xff0c;跑不過不許畢業&#xff0c;取消推研資格。…

信息保真度準則_設計保真度的新的非科學公式

信息保真度準則As designers, our audience is more than just our users. We keep our user’s needs top of mind while designing for their jobs-to-be-done, but sometimes we need to illustrate the problem and articulate solutions to a broader audience: our cross…

zend studio配置調試(Xdebug方式)

1.下載xdebug http://xdebug.org/download.php 我下的是PHP 5.4 VC9 (32 bit) 【當前系統php是php5.4.14(win32)版本】 2.配置php.ini 在文件最后配置 [Xdebug]zend_extension "C:/php5.4.14/ext/php_xdebug-2.2.3-5.4-vc9.dll" xdebug.auto_trace1xdebug.collect_p…

人物肖像速寫_肖像學的基礎

人物肖像速寫More in the iconography series:? 7 Principles of Icon Design? 5 Ways to Create a Settings Icon? Icon Grids & Keylines Demystified? Pixel-Snapping in Icon Design? 3 Classic Icon FamiliesAn icon is a compact symbol that represents a disc…

python處理網絡文字流,設置為utf8編碼

import sysreload(sys)sys.setdefaultencoding(utf8)在python編程時&#xff0c;如果數據來自客戶端傳過來&#xff0c;在使用"%s" % str和json.loads(str)時&#xff0c;會遇到問題。問題的原因是字符集和編碼的問題。以上代碼可以解決這個問題。 轉載于:https://ww…

產品設計的Kawaiization

重點 (Top highlight)在過去的一兩年中&#xff0c;我注意到品牌和產品設計中出現了某種風格。 (Over the last year or two, I’ve noticed a certain style emerge in brand and product design.) (this article originally appeared on DESK magazine)(本文最初出現在DESK雜…

寫了兩個簡單的小工具,文件夾文件操作的

一&#xff0c;文件夾A下的文件夾下的文件&#xff0c;移到文件夾A下。 二&#xff0c;經常上貼吧什么的&#xff0c;有些圖貼&#xff0c;會直接網頁全部保存為&#xff0c;結果是一個html文件&#xff0c;和引用資源的文件夾&#xff0c;文件夾下有很多無用的文件&#xff0c…

前置聲明相關

前置聲明相關 一個前置聲明是指在程序員尚未給出完整定義之前對一個標示符(一個類型、一個變量或者一個函數)的聲明。一個很簡單的例子就是我們在函數A中使用了函數B&#xff0c;但是函數B的聲明在函數A之后&#xff0c;這個時候&#xff0c;就需要對函數B進行前置聲明&#xf…

陌生人社會_陌生人之旅

陌生人社會The Last of Us Part II is a game that is deeply invested in the minutiae of its characters. The pain they cause, the things that drive them, and the particularities of their self-destruction and salvation. The game’s commitment to the true natur…

設計師更高效_要成為更好的設計師,我們需要更多的時間進行游戲

設計師更高效重點 (Top highlight)I’m a busy designer. I’m fortunate to be booked out months in advance. My freelance career has proven more stable than other’s “jobs”. I don’t wear busy as a badge of honour. I don’t condone hustle culture or compare …

java數據類型及其說明

Java數據類型及其說明java基本數據類型&#xff1a; java數據類型分為基本數據類型和引用數據類型&#xff0c;基本數據類型就是4類8種&#xff0c;分為數值類型&#xff08;整數型[byte&#xff0c;short&#xff0c;int&#xff0c;long]&#xff09;&#xff0c;字符型&…

ux設計師薪水_公司與 設計機構:UX設計師的津貼和陷阱

ux設計師薪水Written by Yegor Tsynkevich由Yegor Tsynkevich撰寫 The more companies understand the power of a great user experience design and its impact on customer loyalty, the more they are willing to have it embedded in their culture. With so much emphas…

ZOJ 2165 Red and Black

1.采用dfs&#xff1a; #include <iostream>#include <cstdio>#include <cstring>#include <string>using namespace std;char map[25][25];int count 1;int r,c;int dx[4] {1,-1,0,0};int dy[4] {0,0,1,-1};bool judge(int x,int y){ if(x<0…

java中的equals用法

在Object 類中定義有&#xff1a; 1、public boolean equals(Object object )方法提供定義對象是否“相等”邏輯。 2、Object的equals方法定義為&#xff1a;x.equals(y)當x和y是同一個對象的引用時&#xff0c;返回true&#xff0c;否則返回false 3、在其他一些類中&#xff0…

根據圖片獲得配色方案_配色系列(1)—從圖片中獲得配色靈感

根據圖片獲得配色方案前言 (Foreword) When we start designing mobile web pages, we always need to determine the color scheme of the web page first. Well, at this time, unless the customer proposes a color scheme, most of the situations we have to face is to …

七大查找算法

1. 順序查找2. 二分查找3. 插值查找4. 斐波那契查找5. 樹表查找6. 分塊查找7. 哈希查找 查找是在大量的信息中尋找一個特定的信息元素&#xff0c;在計算機應用中&#xff0c;查找是常用的基本運算&#xff0c;例如編譯程序中符號表的查找。本文簡單概括性的介紹了常見的七種查…

HA2795Billboard 可用線段樹

#include<iostream>using namespace std;#include<cstdio>#include<algorithm>#define maxn 200005int h,n,w;int root[maxn<<4];int ans;//標記void make_tree(int l,int r,int rt){if(lr){root[rt]w;return ;}int mid(rl)/2;make_tree(l,mid,rt*2);m…