c#⽂件上传源代码
⽂件上传这⾥分上传到服务器的⽂件夹和数据库
1:上传到⽂件夹
⾸先看aspx页⾯:
程序代码:
<tr bgcolor="#ffffff">
<td height="24">
<input type="file" id="Up_file" class="edline" runat="server" 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();
asp 源代码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();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论