Asp.net: Two Methods for uploading files

Source: Internet
Author: User

1. Inner type (runat Server Mode)

This method is currently used by most people. The event-driven method is used to upload files. Form and file field must be set to runat server.

The Code is as follows:

Void button1_click (Object source, eventargs e ){

If (file1.postedfile! = NULL ){

// Obtain the upload file type //
String filetype = file1.postedfile. contenttype. tostring ();

// Check whether upload is allowed. Currently, only jpg and GIF files can be uploaded //
If (filetype = "image/pjpeg" | filetype = "image/GIF ")
{
// Get the file name //
String filename = file1.postedfile. filename. tostring ();

// Get the extension //
String fileext = filename. substring (filename. lastindexof ("."));

// Reset the file name //
Datetime now = datetime. now;
String frontfilename = now. tofiletimeutc () + file1.postedfile. contentlength. tostring ();
Filename = "/upload/" + frontfilename + fileext;

Try {
// Save the file
File1.postedfile. saveas (server. mappath (filename ));
Response. Write ("uploaded successfully ");

}
Catch (exception exc ){
Response. Write ("Upload error, cause: +" exc. tostring ()"");
}

}
}

<Form enctype = "multipart/form-Data" runat = "server">
Select the file to be uploaded <input id = "file1" type = file runat = "server">
<Input type = button id = "button1" value = "click to start uploading" onserverclick = "button#click" runat = "server">
</Form>

The reason for this is that the implementation method must be completed in the same aspx file, which has certain restrictions, but is highly secure.

2. Open Mode (submit form Mode)

This method can be submitted by other pages with high freedom. The Code is as follows:

Upload. aspx

<Form name = "form1" Action = "Save. aspx" method = "Post" enctype = "multipart/form-Data">
<Input type = "file" id = "file1" name = "file1">
<Input type = "Submit" name = "Submit" value = "save">
</Form>

Save. aspx

Private void page_load (Object sender, system. eventargs E)
{
If (request. Files. Count = 0 ){
Response. Write ("<SCRIPT> alert ('Select Upload File! '); </SCRIPT> ");
Response. End ();
Return;
}
System. Web. httppostedfile file = request. Files [0];
String filename = request ["FILENAME"];
String save_path = "./attached /";
String ext = system. Io. Path. getextension (file. filename). tolower ();
Response. Write (EXT );
If (! System. Io. Directory. exists (server. mappath (save_path )))
System. Io. Directory. createdirectory (server. mappath (save_path ));
If (EXT = ". jpg" | ext = ". GIF" | ext = ". BMP" | ext = ". PNG "){
File. saveas (server. mappath (save_path + filename ));
Response. write ("<SCRIPT type = \" text/JavaScript \ "> parent. kindinsertimage ('"+ save_path + filename +"', '"+ request [" imgwidth "] +"', '"+ request [" imgheight "] + "', '"+ request [" imgborder "] +"'); </SCRIPT> '");
Response. End ();
}
Else {
Response. Write ("<SCRIPT> alert ('file format not supported! '); </SCRIPT> ");
Response. End ();
}
}

You only need to figure out the two objects httppostedfile and httpfilecollection, and it is not difficult to write batch file uploads.

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.