Save the file to the database

Source: Internet
Author: User

Upload:

Private void uploadfile ()
{
System. Web. httppostedfile file = This. uploadfile. postedfile;
String filename = file. filename;
If (filename. Trim () = "")
{
Webcommon. showmessage (this. Page, "Please input the file name .");
Return;
}
Filename = filename. substring (filename. lastindexof ("\") + 1 );
Int filelength = file. contentlength;
Byte [] filebytes = new byte [filelength];
File. inputstream. Read (filebytes, 0, filelength );
BBA. BLL. teluserlistbiz biz = new teluserlistbiz ();
BBA. model. teluserlistdata DATA = new teluserlistdata ();
Data. filename = filename;
Data. formid = This. lblformid. text;
Data. userlist = filebytes;
Data. discripton = "User List ";

If (biz. exists (data. formid ))
{
Biz. Update (data );
}
Else
{
Biz. insert (data );
}
This. lblfilename. Text = "<br> file name:" + filename + "<br> ";
}

Public bool insert (teluserlistdata data)
{
# Region populate Parameters
Sqlparameter [] arparams = new sqlparameter [4];

Arparams [0] = new sqlparameter ("@ formid", sqldbtype. varchar, 50 );
Arparams [0]. value = data. formid;
Arparams [1] = new sqlparameter ("@ userlist", sqldbtype. Image );
Arparams [1]. value = data. userlist;
Arparams [2] = new sqlparameter ("@ FILENAME", sqldbtype. varchar, 256 );
Arparams [2]. value = data. filename;
Arparams [3] = new sqlparameter ("@ discripton", sqldbtype. varchar, 50 );
Arparams [3]. value = data. discripton;
# Endregion

Sqlhelper. executenonquery (this. connectionstring, commandtype. storedprocedure, "usp_teluserlist_insert", arparams );
Return true;
}
 

 

Download:

Private void btnview_click (Object sender, system. eventargs E)
{
BBA. BLL. teluserlistbiz biz = new teluserlistbiz ();
String formid = This. lblformid. text;
If (biz. exists (formid ))
{
Datatable dt = biz. selectbyformid (formid );
String filename = DT. Rows [0] ["FILENAME"]. tostring ();
Byte [] filebytes = (byte []) dt. Rows [0] ["userlist"];
Int filelength = filebytes. length;
Response. Clear ();
Response. clearheaders ();
Response. Buffer = false;
Response. appendheader ("content-disposition", "attachment; filename =" + filename );
Response. appendheader ("Content-Length", filelength. tostring ());
Response. contenttype = "application/octet-stream ";
Response. binarywrite (filebytes );
Response. Flush ();
Response. End ();
}
Else
{
This. btnview. Enabled = false;
Webcommon. showmessage (this. Page, "file not exists .");
}
}

SQL:

Createprocedure [DBO]. [usp_teluserlist_insert]
@ Formid varchar (50 ),
@ Userlist image,
@ Filename varchar (256 ),
@ Discripton varchar (50)
As
Insert [DBO]. [teluserlist]
(
[Formid],
[Userlist],
[Filename],
[Discripton]
)
Values
(
@ Formid,
@ Userlist,
@ Filename,
@ Discripton
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.