首页 > 编程 > .NET > 正文

asp.net上传下载,自我测试

2024-07-10 13:06:22
字体:
来源:转载
供稿:网友

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
using system.configuration;
using system.io;

namespace sz
{
 /// <summary>
 /// upfile 的摘要说明。
 /// </summary>
 public class upf : system.web.ui.page
 {
  protected system.web.ui.htmlcontrols.htmlinputfile upf1;
  private void page_load(object sender, system.eventargs e)
  {
   

  }
  public void submit_click(object sender,system.eventargs e)
  {
   if(upf1.postedfile.contentlength>0)
   { 
    string[] filesplite = upf1.postedfile.filename.split('//');//取得文件名称
    string fn = filesplite[filesplite.length -1];
    string ft = upf1.postedfile.contenttype;
    fileinfo fi=new fileinfo(upf1.postedfile.filename);
    filestream fs=fi.openread();
    byte[] bytes=new byte[fs.length];
    fs.read(bytes,0,convert.toint32(fs.length));
    string conn = configurationsettings.appsettings["myconn9"];
    sqlconnection cn = new sqlconnection(conn);
    sqlcommand cm=new sqlcommand();
    cm.connection=cn;
    cm.commandtype=commandtype.text;
    if(cn.state==0) cn.open();
    cm.commandtext="insert into sz_file (filename,filetype,filedata) values ('"+fn+"','"+ft+"',@file)";
    sqlparameter spfile=new sqlparameter("@file",sqldbtype.image);
    spfile.value=bytes;
    cm.parameters.add(spfile);
    cm.executenonquery();
   }
  }
  public void down_click(object sender,system.eventargs e){
   string conn = configurationsettings.appsettings["myconn9"];
   string strsql = "select * from sz_file";
   sqlconnection myconnection = new sqlconnection(conn);
   sqlcommand mycommand = new sqlcommand();
   mycommand.connection = myconnection;
   sqldataadapter objdataadapter = new sqldataadapter();
   objdataadapter.selectcommand = mycommand;
   dataset objectset = new dataset();
   mycommand.commandtext = strsql;
   objdataadapter.fill(objectset,"a");
   datarow dr=objectset.tables["a"].rows[0];
   byte[] file=null;
   file=(byte[])dr["filedata"];
   response.addheader("content-disposition", "attachment;filename="+httputility.urlencode(dr["filename"].tostring(),system.text.encoding.utf8));
   response.contenttype=dr["filetype"].tostring();
   response.binarywrite(file);
  }
  #region web 窗体设计器生成的代码
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 该调用是 asp.net web 窗体设计器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {   
   this.load += new system.eventhandler(this.page_load);
  }
  #endregion
 }
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表