c#檔案上傳原始碼

來源:互聯網
上載者:User
檔案上傳這裡分上傳到伺服器的檔案夾和資料庫
1:上傳到檔案夾
首先看aspx頁面:
程式碼: 
<tr bgcolor="#ffffff">
<td height="24">
<input type="file" id="Up_file" class="edline" runat="server" style="WIDTH: 456px; HEIGHT: 20px" size="60" name="Up_file">
</td>
</tr>
<TR bgcolor="#ffffff">
<TD height="24" align="center">
<asp:Button ID="submit" Runat="server" Text="檔案上傳" CssClass="Cmdbut" Height="20px"></asp:Button>
</TD>
</TR> 

下面是後台,及"檔案上傳"按鈕觸發的事件:
程式碼: 
private void submit_Click(object sender, System.EventArgs e)
{
HttpPostedFile postedFile = this.Up_file.PostedFile; //得到要上傳檔案
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName); //檔案名稱
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName); //上傳檔案的副檔名
string new_filename = fileExtension; //給檔案重新命名
//postedFile.FileName: 用戶端檔案地址
//postedFile.ContentType.ToString(): 上傳的檔案類型
//儲存檔案到檔案夾,地址是當前頁面的同一級目錄下的files檔案夾中
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("files/") + new_filename); 
//.....可以把檔案的相應資訊儲存到資料庫中去。
}

在顯示這些檔案資訊的時候,可以直接連結檔案地址。

2:上傳到資料庫
程式碼: 
private void submit_Click(object sender, System.EventArgs e)
{
Stream imgStream;
int docSize;
string docName;
string imgContentType;
string imgUploadedName;
imgStream = this.Up_file.PostedFile.InputStream; //二進位流
docSize = this.Up_file.PostedFile.ContentLength; //檔案大小
imgUploadedName = this.Up_file.PostedFile.FileName; //檔案名稱
byte[] docBody=new byte[docSize];
imgContentType = this.Up_file.PostedFile.ContentType; //檔案類型
docName =imgUploadedName.Substring(imgUploadedName.LastIndexOf('\\')+1);
int n = imgStream.Read(docBody, 0, docSize); 
DataTable temp = mynew.Get_ZH_EngBriefing_byoid(0); //返回一個空的table
DataRow row = temp.NewRow();
//.....省略其他儲存代碼
row["DOWN_PATH"] = docBody; //儲存資料到資料庫,DOWN_PATH欄位為二進位
row["FILENAME"] = docName; //儲存檔案名稱
row["LX"] = imgContentType; //儲存檔案類型
temp.Rows.Add(row);
mynew.SaveZHEngBriefing(temp); 

在顯示這些檔案資訊時,如果要實現下載,可以參考下面的代碼:
1):當放置到datagrid中時,必須綁定資料到模板列,比如:
程式碼: 
<asp:TemplateColumn HeaderText="檔案下載">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:LinkButton id=downLink runat="server" ToolTip='<%# "下載檔案:"+DataBinder.Eval(Container.DataItem,"FILENAME").ToString() %>' Text='<%# DataBinder.Eval(Container.DataItem,"FILENAME").ToString() %>' CommandName="downFile">
</asp:LinkButton>
</ItemTemplate>
<FooterStyle HorizontalAlign="Center"></FooterStyle>
</asp:TemplateColumn> 

利用datagrid的ItemCommand事件來實現下載
程式碼: 
private void Eng_briefing_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName == "downFile")
{
long EngBriefing_OID = long.Parse(Eng_briefing.Items[e.Item.ItemIndex].Cells[0].Text.ToString()); //首先返回要下載資料的相應行記錄關鍵字OID
DataTable file = mynew.Get_ZH_EngBriefing_byoid(EngBriefing_OID); //擷取該檔案所在行記錄(這裡Get_ZH_EngBriefing_byoid方法是我自己的一個定義的根據OID來返回table)
if (file.Rows.Count > 0)
{
Response.Clear();
Response.Buffer = false;
Response.AppendHeader("Content-Disposition","attachment; filename="+HttpUtility.UrlEncode(file.Rows[0]["FILENAME"].ToString(),System.Text.Encoding.UTF8));
Response.BinaryWrite((byte[])file.Rows[0]["DOWN_PATH"]);
Response.End(); 
}

}

當檔案資訊以一個LinkButton顯示出來的話,就可以利用lLinkButton的onclick事件來實現:
程式碼: 
<asp:linkbutton id="FileLink" runat="server"></asp:linkbutton> 

程式碼: 
private void FileLink_Click(object sender, System.EventArgs e)
{
long EngBriefing_OID = long.Parse(this.L_EngBriefing_OID.Text.ToString()); //取得檔案所在行的相應OID
DataTable file = mynew.Get_ZH_EngBriefing_byoid(EngBriefing_OID); //擷取檔案所在行資訊
if (file.Rows.Count > 0)
{
Response.Clear();
Response.Buffer = false;
Response.AppendHeader("Content-Disposition","attachment; filename="+HttpUtility.UrlEncode(file.Rows[0]["FILENAME"].ToString(),System.Text.Encoding.UTF8));
Response.BinaryWrite((byte[])file.Rows[0]["DOWN_PATH"]);
Response.End(); 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.