Three methods for uploading asp.net files

Source: Internet
Author: User

Asp tutorial. net file upload methods

<% @ Page language = "C #" %>
<% @ Import Namespace = "System. IO" %>

<Script runat = "server">
Void UploadButton_Click (object sender, EventArgs e)
    {
String savePath = @ "c :";
If (! Directory. Exists (savePath )){
String msg = "Response. Write (String. Format (msg, savePath ));
Response. End ();
        }
If (FileUpload1.PostedFile! = Null)
        {
String fileName = Path. GetFileName (FileUpload1.Value );

SavePath + = fileName;

FileUpload1.PostedFile. SaveAs (savePath );
UploadStatusLabel. InnerText = "File saved as: <I>" + savePath + "</I> ";
        }
Else
        {
UploadStatusLabel. InnerText = "You did not specify a file to upload .";
        }
    }
</Script>

<Html xmlns = "http://www.111cn.net">
<Head runat = "server">
<Title> HtmlInputFile </title>
</Head>
<Body>
<Div id = "pageContent">

<Form ID = "Form1" runat = "server">
<H3> Select a picture to upload:
<Hr/>
<B> Picture to upload </B> <br/>
<Input type = "file" id = "FileUpload1" runat = "server"/>
<Br/>
<Input runat = "server" id = "UploadButton" type = "submit" value = "Upload"
Onserverclick = "UploadButton_Click"/>
<Hr/>
<Span runat = "server" id = "UploadStatusLabel"/>
</Form>
</Div>
</Body>
</Html>

Method 2

<Script language = "C #" runat = "server">
Protected void PhotoSubmit (object o, EventArgs e ){
If (thePhoto. PostedFile! = Null ){
Try {
String filepath = "C: temp" + DateTime. Now. Ticks. ToString ();
ThePhoto. PostedFile. SaveAs (filepath );
Status. Text = "File saved as" + filepath;
           
        }
Catch (Exception exc ){
Status. Text = "An Error occurred processing the file. Please try again." + exc. ToString ();
        }
    }
}
</Script>
<Form runat = "server" enctype = "multipart/form-data">
Please select an image to submit: <input id = "thePhoto" type = "file" runat = "server"> <br/>
<Input type = "button" runat = "server" value = "Proceed" OnServerClick = "PhotoSubmit"/>
<Asp: label runat = "server" id = "status"/>
</Form>

Method 3

<% @ Page Language = "C #" %>
<Script runat = "server">
Void UploadButton_Click (Object sender, EventArgs e ){
If (FileInput. PostedFile! = Null)
        {
Try
            {
FileInput. PostedFile. SaveAs ("c: test.txt ");
            }
Catch (Exception exc)
            {
Response. Write (exc. Message );
            }
        }
    }
</Script>
<Html>
<Head>
</Head>
<Body>
<Form id = "Form1" method = "post" enctype = "multipart/form-data" runat = "server">
<Input id = "FileInput" type = "file" runat = "server"/>
<Asp: Button id = "UploadButton" onclick = "UploadButton_Click" runat = "server" Text = "Upload"> </asp: Button>
</Form>
</Body>
</Html>

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.