Two methods of implementing Web file uploading in C #

Source: Internet
Author: User

In web programming, we often need to upload some local files on the Web server, after uploading, users can easily browse these files through the browser, the application is very extensive.

So how do you use C # to implement file upload functions? The following is a brief introduction.

First, add an uploaded Web form to your Visual C # Web project, and in order to upload the file, you need to select the HTML class's File field control in Toolbox to add the control to the Web form. At this point, however, the control is not a server-side control, and we need to add the following code to it:

<form method=post encType=multipart/ form-data runat="server">

So that it becomes a server-side control, and if you need to upload several files at the same time, we can add this control accordingly.

It is important to note that the attribute settings in the code must be set to:

<form method=post encType=multipart/ form-data runat="server">

Without this attribute, uploads cannot be implemented.

Then add a button for the Web Form class in this Web Form and double click the button for the following code:

//upload the picture's program segment
DateTime now = DateTime.Now
//Take the time to the Datatime class's object today
String strbaselocation = "D:\\web \\FC\\pic\\ ";
//This is the absolute directory of the server to which the file will be uploaded
if (uploadfile1. Postedfile.contentlength!= 0
//Determines whether the selection dialog box chooses a file length of 0
{
Uploadfile1. Postedfile.saveas (Strbaselocation+now.
Dayofyear.tostring () +uploadfile1. PostedFile.
Contentlength.tostring () + ". jpg");
//execute upload and automatically name files based on date and file size, make sure not to repeat
label1.text= "Picture 1 has been uploaded, the file name is:" +now. DayOfYear.
ToString () +uploadfile1. PostedFile.ContentLength.ToString () + ". jpg";
Navigator.   Insert (System.Xml.TreePosition.After,
XmlNodeType.Element, "Pic1", "", "");
Navigator.   Insert (System.Xml.TreePosition.FirstChild,
XmlNodeType.Text, "Pic1", "", "");
Navigator. Value= now. Dayofyear.tostring () +uploadfile1.
PostedFile.ContentLength.ToString () + ". jpg";
Navigator. Movetoparent ();
}

The above code for the author developed a use of XML files to store news information System, the next few lines of code to write upload file information to the XML file. If you want to upload other types of files, just need to change JPG to the appropriate type of suffix name, such as doc can upload Word file, browser can directly browse the uploaded Word file.

"Precautions"

1. Upload file can not be unlimited;

2. Be aware of the security aspects of IIS coordination;

3. When installing programs with visual Studio installations, be aware of the absolute path problem of the installer;

4. Note the name of the file upload after the problem.

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.