熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

上傳文件到數據庫並從數據庫讀出

2022-06-13   來源: .NET編程 

  老是找不到合適的數據庫上傳下載的從網上找了一些代碼改寫了個可以直接使用的版本!

  代碼

  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();

  }

  }

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/13587.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.