本文主要介紹一下,在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();}}
以上就完成了,簡單的文件上傳與下載。
?
?