ASP. NET file upload and download code

Source: Internet
Author: User

Upload: (multiple files can be uploaded in sequence)

Front-end:

<% @ Register tagprefix = "ew1" namespace = "eWorld. UI" assembly = "eWorld. UI, version = 1.9.0.0, culture = neutral, publickeytoken = 24d65337282035f2" %>
<% @ Page Language = "C #" codebehind = "webnewbid. aspx. cs" autoeventwireup = "false" inherits = "star. Web. Main. Bidding. webnewbid" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webnewbid </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "C #" name = "code_language">
<Meta content = "JavaScript" name = "vs_defaultclientscript">
<Meta content ="Http://schemas.microsoft.com/intellisense/ie5"Name =" vs_targetschema ">
<Link href = ".../../CSS/style.css" type = "text/CSS" rel = "stylesheet">
<Script language = "JavaScript">
Function AddFile ()
{
VaR STR = '<br> <input type = "file" size = "50" name = "file" runat = "server">'
Document. getelementbyid ('myfile'). insertadjacenthtml ("beforeend", STR)
}
</SCRIPT>
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Table class = "fullwidth" align = "center">
<Tr>
<TD valign = "TOP"> attachment: </TD>
<TD>
<P id = "myfile"> <input id = "filmyfile" type = "file" size = "50" name = "filmyfile"> & nbsp; <input onclick = "AddFile ()" type = "button" value = "add"> </P>
<Asp: Label id = "lblattachmenterror" runat = "server" forecolor = "red"> </ASP: Label> <br>
<Asp: button id = "btnupload" runat = "server" text = "Upload"> </ASP: button> <asp: label id = "lblattachment" runat = "server"> </ASP: Label> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

Background:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
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. Collections. Specialized;
Private void btnupload_click (Object sender, system. eventargs E)
{
Httpfilecollection files = httpcontext. Current. Request. files;
For (INT I = 0; I <files. Count; I ++)
{
If (I <files. Count & I <10)
{
If (files [I]. filename! = "" | Files [I]! = NULL)
{
Int filesize = 6*1024*1024;
Httppostedfile myfile = files [I];
String strfilepath = myfile. filename. tostring (). Trim ();
This. lblattachmenterror. Text = "<" + strfilepath + ">"; // show file name
Int nfindslashpos = strfilepath. Trim (). lastindexof ("// ") + 1;
String uploadfilename = strfilepath. substring (nfindslashpos );
String filename=this.txt workorder. Text + "_" + String. Format ("{0: yymmdd-hhmmss}", datetime. Now) + "_" + uploadfilename;

If (myfile. filename. Trim () = "") // empty value in browse box
{
This. lblattachmenterror. Text = "no file selected .";
Return;
}

If (myfile. contentlength! = 0)
{
If (myfile. contentlength> filesize)
{
This. lblattachmenterror. Text = "file size is limited to 6 MB only .";
Return;
}
This. lblattachment. Text + = "<br>" + filename;
This. lblattachmenterror. Text = "";
// String S = This. Request. physicalapplicationpath. tostring (). Trim ();
// String S1 = This. Request. applicationpath. tostring (). Trim ();
// String S3 = This. server. mappath ("");
Myfile. saveas (this. Request. physicalapplicationpath. tostring (). Trim () + @ "/uploads/" + filename );
Arrayfilename [I] = filename;
//// Return;
}
Else
{
This. lblattachmenterror. Text = "file not found .";
Return;
}
}
}
Else
This. lblattachmenterror. Text = "uploaded file exceed limits .";
}
}

Download: (I only gave the latter code)

Public bool responsefile (httprequest _ Request, httpresponse _ response, string _ filename, string _ fullpath, long _ speed)
{
Try {
Filestream myfile = new filestream (_ fullpath, filemode. Open, fileaccess. Read, fileshare. readwrite );
Binaryreader BR = new binaryreader (myfile );
Try {
_ Response. addheader ("Accept-ranges", "bytes ");
_ Response. Buffer = false;
Long filelength = myfile. length;
Long startbytes = 0;
Int16 Packs = 10240;
Int16 sleep = convert. toint16 (math. Floor (1000 * pack/_ speed) + 1 );
If (! (_ Request. headers ("range") = NULL )){
_ Response. statuscode = 206;
Char [] splitchar = new char [1];
Splitchar (0) = "= ";
Splitchar (1) = "-";
String [] [0] range = _ request. headers ("range"). Split ("");
Startbytes = convert. toint64 (range (1 ));
}
_ Response. addheader ("Content-Length", (filelength-startbytes). tostring ());
If (startbytes! = 0 )){
_ Response. addheader ("content-range", String. Format ("bytes {0}-{1}/{2}", startbytes, filelength-1, filelength ));
}
_ Response. addheader ("connection", "keep-alive ");
_ Response. contenttype = "application/octet-stream ";
_ Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (_ filename, system. Text. encoding. utf8 ));
BR. basestream. Seek (startbytes, seekorigin. Begin );
Long maxcount = convert. toint64 (math. Floor (filelength-startbytes)/Pack) + 1 );
Long I;
For (INT I = 0; I <= maxcount; I ++ ){
If (_ response. isclientconnected )){
_ Response. binarywrite (Br. readbytes (pack ));
Thread. Sleep (sleep );
} Else {
I = maxcount;
}
}
} Catch (exception e ){
Return false;
} Finally {
BR. Close ();
Myfile. Close ();
}
} Catch {
Return false;
}
}

Related Article

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.