Upload and download files

Source: Internet
Author: User
// Upload the Code:

Protected void sh_click (Object sender, eventargs E)
{

String filename = This. fileupload1.filename. Trim ();
String Path = "wenjian/" + filename; // put the uploaded file in the folder named wenjian.
String A = server. mappath ("./"); // obtain the Directory
If (! Directory. exists (a + "wenjian") // create a folder if the folder does not exist
{
Directory. createdirectory (a + "wenjian ");
}

Fileupload1.postedfile. saveas (a + path); // put the uploaded file in the specified path
Page. clientscript. registerstartupscript (GetType (), "", "<SCRIPT> alert ('upload successful! ') </SCRIPT> ");
}

// Download the Code:

Protected void down_click (Object sender, eventargs E)
{
String fileurl = server. mappath ("wenjian/" + "Amy gao_201107 1.docx ");
Fileurl = fileurl. Replace (@ "\ wenjian", @ "\ wenjian"); // cancel invalid characters

Fileinfo = new fileinfo (fileurl );
Response. Clear ();
Response. clearcontent ();
Response. clearheaders ();
Response. addheader ("content-disposition", "attachment; filename =" + server. urlpathencode (fileurl ));
Response. addheader ("Content-Length", fileinfo. length. tostring ());
Response. addheader ("content-transfer-encoding", "binary ");
Response. contenttype = "application/octet-stream ";
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Response. writefile (fileinfo. fullname );
Response. Flush ();
Response. End ();
}

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.