知识大全 上传文件到数据库并从数据库读出
Posted 附件
篇首语:讨厌自己明明不甘平凡,却又不好好努力。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 上传文件到数据库并从数据库读出相关的知识,希望对你有一定的参考价值。
老是找不到合适的数据库上传下载的 从网上找了一些代码 改写了个可以直接使用的版本!
代码
AttachmentId bigint Unchecked
AttachmentName nvarchar( ) Unchecked
AgencyName nvarchar( ) Checked
ClassName nvarchar( ) Checked
TypeName nvarchar( ) Checked
Image image Checked
Flag int Checked
Notes text Checked
Unchecked
<%@ Page Language= C# AutoEventWireup= true CodeBehind= Attachment aspx cs Inherits= hrootCIS DataShow Types NewNavigator Attachment %>
<%@ Register Assembly= AjaxControlToolkit Namespace= AjaxControlToolkit TagPrefix= cc %>
<!DOCTYPE PUBLIC //W C//DTD XHTML Transitional//EN transitional dtd >
< xmlns= >
<head id= Head runat= server >
<title>附件信息</title>
<link href = type= text/css rel= stylesheet />
</head>
<body>
<form id= form runat= server >
<div >
<asp:ScriptManager ID= ScriptManager runat= server >
</asp:ScriptManager>
<cc :TabContainer ID= TabContainer runat= server ActiveTabIndex=
Height= px >
<cc :TabPanel runat= server HeaderText= 添加附件信息 ID= TabPanel >
<HeaderTemplate>
添加附件信息
</HeaderTemplate>
<ContentTemplate>
<div>
<asp:Label ID= lblPoistion runat= server Text= 附件 ></asp:Label>
<input id= UpFile runat= server name= File
type= file /><br />
<br />
<br />
<table>
<tr>
<td>选择公司 </td>
<td><asp:DropDownList ID= dplAgencyA runat= server
DataSourceID= SqlDataSourceAgencyA DataTextField= AgencyName
DataValueField= Agencyid ></asp:DropDownList><asp:SqlDataSource ID= SqlDataSourceAgencyA runat= server
ConnectionString= <%$ ConnectionStrings:mvcNewsConnectionString %>
SelectCommand= SELECT * FROM [Agency] ></asp:SqlDataSource></td></tr>
<tr>
<td>附件所属类别:</td>
<td> <asp:DropDownList ID= dplClass runat= server >
<asp:ListItem>企业研究报告</asp:ListItem>
<asp:ListItem>广告投放信息</asp:ListItem>
<asp:ListItem>媒体曝光信息</asp:ListItem>
<asp:ListItem>行业调研报告</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>附件简介</td>
<td>
<asp:TextBox ID= txtNotes runat= server Height= px TextMode= MultiLine
Width= px ></asp:TextBox> </td>
</tr>
</table>
<asp:Button ID= btnUpLoad runat= server Text= 上传附件
onclick= btnUpLoad_Click />
</div>
</ContentTemplate>
</cc :TabPanel>
<cc :TabPanel ID= TabPanel runat= server HeaderText= 管理 >
<HeaderTemplate >
管理附件信息
</HeaderTemplate>
<ContentTemplate >
<table cellpadding= cellspacing= width= % ><tr><td><asp:DropDownList ID= DropDownList runat= server
DataSourceID= SqlDataSourceAgencyB DataTextField= AgencyName
DataValueField= Agencyid ></asp:DropDownList><asp:SqlDataSource ID= SqlDataSourceAgencyB runat= server
ConnectionString= <%$ ConnectionStrings:mvcNewsConnectionString %>
SelectCommand= SELECT * FROM [Agency] ></asp:SqlDataSource></td></tr><tr><td>
<asp:Button
ID= BtnShowAttachment runat= server Text= 显示所选公司的附件信息
onclick= BtnShowAttachment_Click BorderStyle= Outset
BorderWidth= px /></td></tr><tr><td align= center ><asp:GridView ID= gdvAttachment runat= server AutoGenerateColumns= False
Width= px CellPadding= ForeColor= # GridLines= None ><AlternatingRowStyle BackColor= White /><Columns><asp:TemplateField
HeaderText= ID Visible= False ><ItemTemplate><asp:Label ID= lblId
runat= server Text= <%#Bind( AttachmentId ) %> ></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField><ItemTemplate><%#Eval( AttachmentName )%></a></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText= 选项 ><ItemTemplate><asp:CheckBox ID= cDelete runat= server /></ItemTemplate></asp:TemplateField><asp:TemplateField></asp:TemplateField></Columns><EditRowStyle BackColor= # BF /><FooterStyle BackColor= # CD Font Bold= True ForeColor= White /><HeaderStyle BackColor= # CD Font Bold= True ForeColor= White /><PagerStyle BackColor= # BF ForeColor= White HorizontalAlign= Center /><RowStyle BackColor= #EFF FB /><SelectedRowStyle BackColor= #D DDF Font Bold= True ForeColor= # /></asp:GridView></td></tr><table><tr><td><asp:Button ID= btnDelete runat= server OnClick= btnDelete_Click Text= 删除 /></td></tr></table></table>
</ContentTemplate>
</cc :TabPanel>
<cc :TabPanel ID= TabPanel runat= server HeaderText= 下载附件 >
<HeaderTemplate >
下载附件
</HeaderTemplate>
<ContentTemplate >
<asp:GridView ID= gdvAttachmentA runat= server AutoGenerateColumns= False
DataSourceID= SqlDataSourceAttachment
onrowmand= gdvAttachmentA_RowCommand DataKeyNames= AttachmentId >
<Columns >
<asp:BoundField DataField= AttachmentName HeaderText= 文件名称
SortExpression= AttachmentName />
<asp:BoundField DataField= ClassName HeaderText= ClassName
SortExpression= ClassName />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID= lbtnDownload runat= server CommandName= download CommandArgument= <%#Eval( AttachmentName ) %> >下载</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID= SqlDataSourceAttachment runat= server
ConnectionString= <%$ ConnectionStrings:mvcNewsConnectionString %>
SelectCommand= SELECT AttachmentId AttachmentName ClassName FROM [Attachment] ></asp:SqlDataSource>
</ContentTemplate>
</cc :TabPanel>
</cc :TabContainer>
</div>
</form>
</body>
</>
代码
using System;
using System Collections Generic;
using System Linq;
using System Web;
using System Web UI;
using System Web UI WebControls;
using System Data;
using System Configuration;
using System Collections;
using System Web Security;
using System Web UI WebControls WebParts;
using System Web UI HtmlControls;
using System IO;
using System Web SessionState;
using System Drawing;
using System Data SqlClient;
using hrootCIS DataCollection;
namespace hrootCIS DataShow Types NewNavigator
public partial class Attachment : System Web UI Page
string constring = ConfigurationManager ConnectionStrings[ connectionString ] ToString();
protected void Page_Load(object sender EventArgs e)
protected void btnUpLoad_Click(object sender EventArgs e)
//文件名称
string path = this UpFile PostedFile FileName;
string filename = path Substring(path LastIndexOf( \\\\ ) + );
//文件大小
int size = this UpFile PostedFile ContentLength;
//文件类型
string type = this UpFile PostedFile ContentType;
Response Write( path= + path + <br/>filename= + filename + size= + size + type= + type);
//if (!(type == image/gif || type == image/pjpeg || type == image/bmp ))
// Response Write( <script language= javascript >alert( 请上传附件格式 );</script> );
//else
//
try
Stream ImageStream = UpFile PostedFile InputStream;
byte[] Content = new byte[size];
int Status = ImageStream Read(Content size);
AttachmentInfo info = new AttachmentInfo();
info AttachmentName = filename;
info AgencyName = this dplAgencyA SelectedItem Text;
info ClassName = this dplClass SelectedItem Text;
info TypeName = type;
info Image = Content;
info Flag = ;
info Notes = this txtNotes Text;
AttachmentToData AddAttachment(info);
Response Write( <br/><font color=\\ green\\ size=\\ \\ >上传成功</font> );
catch (Exception ex)
Response Write(ex ToString());
Response Write( <script language= javascript >alert( 请不要给同一个企业上传同名附件 );</script> );
//
protected void BtnShowAttachment_Click(object sender EventArgs e)
BindGrivdView();
protected void btnDelete_Click(object sender EventArgs e)
foreach (GridViewRow row in this gdvAttachment Rows)
if (((CheckBox)row FindControl( cDelete )) Checked)
int id = int Parse(((Label)row FindControl( lblId )) Text);
this BulletinDelete(id);
BindGrivdView();
protected void BulletinDelete(int id)
//Response Write( id= + id);
AttachmentToData DeleteAttachmentById(id);
/// <summary>
/// 根据所选择的公司名称 显示出相关的签约信息
/// </summary>
protected void BindGrivdView()
List<AttachmentInfo> myMeida = AttachmentToData GetAttachmentListByAgencyName(this dplAgencyA SelectedItem Text);
this gdvAttachment DataSource = myMeida;
this gdvAttachment DataBind();
protected void gdvAttachmentA_RowCommand(object sender GridViewCommandEventArgs e)
if (e CommandName == download )
byte[] fileData = null;
using (SqlConnection cn = new SqlConnection(System Configuration ConfigurationManager ConnectionStrings[ connectionString ] ConnectionString))
SqlCommand cmd = new SqlCommand( select Image from Attachment where AttachmentId = @AttachmentId cn);
int index = ((e CommandSource as LinkButton) NamingContainer as GridViewRow) RowIndex;
cmd Parameters AddWithValue( @AttachmentId gdvAttachmentA DataKeys[index] Value);
cn Open();
fileData = cmd ExecuteScalar() as byte[];
if (fileData == null)
Response StatusCode = ;
else
//使用Content Disposition会有些缺点 在不同的浏览器中文件名显示有些不正常 如果用FireFox就无须编码
Response AddHeader( Content Disposition attachment; filename=\\ + HttpUtility UrlEncode(e CommandArgument ToString() System Text Encoding UTF ) + \\ );
Response BinaryWrite(fileData);
Response End();
cha138/Article/program/net/201311/13587
相关参考
ASP.NET2.0中将文件上传到数据库 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 此问题经
知识大全 servlet实现从oracle数据库的blob字段中读出文件并显示
/* *ReadFilejava * *Createdon年月日下午: */ packagejgwlapp; importjavaio*;import*
近段要实现一个上传excel文件到服务器并把excel文件的数据导入到数据库表中的功能 excel文件有两种格式对应数据库两张表我先实现了其中一种excel格式式
从文件读数据要比直接从数据库快测试读出条新闻读数据库用了毫秒读文件只用了毫秒下例为使用文件缓存的代码 使用方法在需要使用文件缓存的页面加入下列代码 <!#incl
.你在页面中写下面的语句就可以把文件数据传到servlet中 <%@pagecontentType="text/;charset=gb2312"%>
机组检修前,软件或数据备份结束后,还应做的工作有()A、在备份上正确标明编号、名称、备份时间等内容;B、对备份进行读出质量检查,应无介质损坏或不能读出等现象发生,备份的内容、文件大小和日期等应正确C、
机组检修前,软件或数据备份结束后,还应做的工作有()A、在备份上正确标明编号、名称、备份时间等内容;B、对备份进行读出质量检查,应无介质损坏或不能读出等现象发生,备份的内容、文件大小和日期等应正确C、
大型的数据库开发中常常遇到数据源是平面文件的情况对于这样的数据源无法使用数据库对其数据进行有效的管理 本文介绍几种常见的数据导入的方法希望能够给读者以启迪另外本文所涉及到的数据库均为ORACLE
知识大全 DELPHI存取JPEG文件到SQL Server数据库
DELPHI存取JPEG文件到SQLServer数据库 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
利用RMAN将数据库从文件系统迁移到ASM 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 一打开