Asp.net upload/download, self-testing

Source: Internet
Author: User

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>
/// Summary of 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 ('//'); // get the file name
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. receivettings ["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. receivettings ["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, "");
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 );
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}

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.