ASP. NET upload file instance code

Source: Internet
Author: User

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "fileupload. aspx. cs" inherits = "em_fileupload" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Upload File </title>
<Base target = _ Self/>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "margin: 10px;">
<Fieldset style = "width: 300px;"> <legend style = "font-size: small;"> Upload File </legend>
<Table Style = "width: 300px;">
<Tr>
<TD>
<Asp: Label id = "label1" runat = "server" text = "Please control the hosts file within 10 MB! "Font-size =" small "forecolor =" red "> </ASP: Label> </TD>
</Tr>
<Tr>
<TD style = "width: 300px">
<Asp: fileupload id = "fuselect" runat = "server" width = "296px"/> </TD>
</Tr>
<Tr>
<TD style = "width: 300px">
& Nbsp; & nbsp;
<Asp: button id = "btnyes" runat = "server" text = "upper limit" tooltip = "confirm to use this file and exit! "
Onclick = "btnyes_click"/>
& Nbsp;
<Asp: button id = "btnno" runat = "server" text = "" tooltip = "and exit"
Onclick = "btnno_click"/>
</TD>
</Tr>
</Table>

</Fieldset>
</Div>
</Form>
</Body>
</Html>

 

 

// ********** CS

 

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class em_fileupload: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Response. expires = 0;
Response. cache. setnostore ();
Response. appendheader ("Pragma", "No-Cache ");

}
Protected void btnyes_click (Object sender, eventargs E)
{
If (fuselect. hasfile)
{
Try
{
String filepath = system. Web. httpcontext. Current. Request. mappath ("datum") + "\" + request. querystring ["shipid"]. tostring ();
If (! System. Io. Directory. exists (filepath ))
System. Io. Directory. createdirectory (filepath );

Fuselect. saveas (filepath + "\" + fuselect. filename );
// Label2.text = ":" + fuselect. postedfile. filename + "<br>" +
// "File name:" + system. Io. Path. getfilename (fuselect. filename) + "<br>" +
// "File size:" + fuselect. postedfile. contentlength + "kb <br> ";
// Label1.text = system. Io. Path. getfilename (fuselect. filename). tostring (). Trim ();
// Session ["shipment"] = system. Io. Path. getfilename (fuselect. filename). tostring (). Trim ();
// Ibtnyes. Enabled = true;
}
Catch (exception ex)
{
Label1.text = "sending response failed:" + ex. Message. tostring ();
Return;
// Ibtnyes. Enabled = false;
}
}
Else
{
Label1.text = "Please select the file to be uploaded! ";
Return;

}

// Page. registerstartupscript ("mobilemsg", "<SCRIPT> specify dialogarguments.doc ument. getelementbyid ('ctl00 _ contentplaceholder=lblequphoto '). innerhtml = '"+ label1.text. tostring () + "'; </SCRIPT> ");
Page. registerstartupscript ("cancelselect", "<SCRIPT> self. Close (); </SCRIPT> ");
}
Protected void btnno_click (Object sender, eventargs E)
{
Page. registerstartupscript ("cancelselect", "<SCRIPT> self. Close (); </SCRIPT> ");
}
}

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.