数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

上传文件到数据库并从数据库读出


发布日期:2022年10月08日
 
上传文件到数据库并从数据库读出

老是找不到合适的数据库上传下载的从网上找了一些代码改写了个可以直接使用的版本!

代码

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=Attachmentaspxcs Inherits=hrootCISDataShowTypesNewNavigatorAttachment %>

<%@ Register Assembly=AjaxControlToolkit Namespace=AjaxControlToolkit TagPrefix=cc %>

<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

<html 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=chkDelete runat=server /></ItemTemplate></asp:TemplateField><asp:TemplateField></asp:TemplateField></Columns><EditRowStyle BackColor=#BF /><FooterStyle BackColor=#CD FontBold=True ForeColor=White /><HeaderStyle BackColor=#CD FontBold=True ForeColor=White /><PagerStyle BackColor=#BF ForeColor=White HorizontalAlign=Center /><RowStyle BackColor=#EFFFB /><SelectedRowStyle BackColor=#DDDF FontBold=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

onrowcommand=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 AttachmentIdAttachmentNameClassName FROM [Attachment]></asp:SqlDataSource>

</ContentTemplate>

</cc:TabPanel>

</cc:TabContainer>

</div>

</form>

</body>

</html>

代码

using System;

using SystemCollectionsGeneric;

using SystemLinq;

using SystemWeb;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemData;

using SystemConfiguration;

using SystemCollections;

using SystemWebSecurity;

using SystemWebUIWebControlsWebParts;

using SystemWebUIHtmlControls;

using SystemIO;

using SystemWebSessionState;

using SystemDrawing;

using SystemDataSqlClient;

using hrootCISDataCollection;

namespace hrootCISDataShowTypesNewNavigator

{

public partial class Attachment : SystemWebUIPage

{

string constring = ConfigurationManagerConnectionStrings[connectionString]ToString();

protected void Page_Load(object sender EventArgs e)

{

}

protected void btnUpLoad_Click(object sender EventArgs e)

{

//文件名称

string path = thisUpFilePostedFileFileName;

string filename = pathSubstring(pathLastIndexOf(\\) + );

//文件大小

int size = thisUpFilePostedFileContentLength;

//文件类型

string type = thisUpFilePostedFileContentType;

ResponseWrite(path= + path + <br/>filename= + filename + size= + size + type= + type);

//if (!(type == image/gif || type == image/pjpeg || type == image/bmp))

// ResponseWrite(<script language=javascript>alert(请上传附件格式);</script>);

//else

// {

try

{

Stream ImageStream = UpFilePostedFileInputStream;

byte[] Content = new byte[size];

int Status = ImageStreamRead(Content size);

AttachmentInfo info = new AttachmentInfo();

infoAttachmentName = filename;

infoAgencyName = thisdplAgencyASelectedItemText;

infoClassName = thisdplClassSelectedItemText;

infoTypeName = type;

infoImage = Content;

infoFlag = ;

infoNotes = thistxtNotesText;

AttachmentToDataAddAttachment(info);

ResponseWrite(<br/><font color=\green\ size=\\>上传成功</font>);

}

catch (Exception ex)

{

ResponseWrite(exToString());

ResponseWrite(<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 thisgdvAttachmentRows)

{

if (((CheckBox)rowFindControl(chkDelete))Checked)

{

int id = intParse(((Label)rowFindControl(lblId))Text);

thisBulletinDelete(id);

}

}

BindGrivdView();

}

protected void BulletinDelete(int id)

{

//ResponseWrite(id= + id);

AttachmentToDataDeleteAttachmentById(id);

}

/// <summary>

/// 根据所选择的公司名称 显示出相关的签约信息

/// </summary>

protected void BindGrivdView()

{

List<AttachmentInfo> myMeida = AttachmentToDataGetAttachmentListByAgencyName(thisdplAgencyASelectedItemText);

thisgdvAttachmentDataSource = myMeida;

thisgdvAttachmentDataBind();

}

protected void gdvAttachmentA_RowCommand(object sender GridViewCommandEventArgs e)

{

if (eCommandName == download)

{

byte[] fileData = null;

using (SqlConnection cn = new SqlConnection(SystemConfigurationConfigurationManagerConnectionStrings[connectionString]ConnectionString))

{

SqlCommand cmd = new SqlCommand(select Image from Attachment where AttachmentId = @AttachmentId cn);

int index = ((eCommandSource as LinkButton)NamingContainer as GridViewRow)RowIndex;

cmdParametersAddWithValue(@AttachmentId gdvAttachmentADataKeys[index]Value);

cnOpen();

fileData = cmdExecuteScalar() as byte[];

}

if (fileData == null)

{

ResponseStatusCode = ;

}

else

{

//使用ContentDisposition会有些缺点在不同的浏览器中文件名显示有些不正常如果用FireFox就无须编码

ResponseAddHeader(ContentDisposition attachment; filename=\ + HttpUtilityUrlEncode(eCommandArgumentToString() SystemTextEncodingUTF) + \);

ResponseBinaryWrite(fileData);

}

ResponseEnd();

}

}

}

}

               

上一篇:使用共享内存在进程之间传送数据

下一篇:创建一个三层的数据库应用程序