Implementation of web file upload with C #

Source: Internet
Author: User
web| upload in the 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 a Web form to upload in 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, but at this point the control is not Server-side control, we need to add the following code to it:

It is important to note that the code must set the property of

to be:


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

Upload a picture of the program section

DateTime now = DateTime.Now;

Take the current time to the object in the Datatime class now

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)//Determine if the selection dialog box chooses a file length of 0

{Uploadfile1. Postedfile.saveas (Strbaselocation+now. Dayofyear.tostring () +uploadfile1. PostedFile.ContentLength.ToString () + ". jpg");

Perform uploads and automatically name files based on date and file size to ensure that they are not duplicated

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.