Chapter 6. File Upload

Source: Internet
Author: User
Tags add time server memory

Steps:

1. Write out the relative path position in the project first

String path = "Images/aaa.txt";

2. Map this relative path to absolute path

String endpath = Server.MapPath (path);

3. Save

Fileupload1.saveas ("Endpath");

Issue 1: file name is the same, easy to be overwritten

Method: Keep The original name of the file
String path = "images/" +fileupload1.filename;

Issue 2: file name is the same, multiple people upload the same file will be overwritten

Method: Name Add time splicing, user name splicing

String path= "images/" +datetime.now.tostring ("Yyyymmddhhmmss") +request.cookies["User". Value+fileupload1.filename;

Issue 3: Upload file format restrictions

Option 1:

Control properties: Accept = ". Jpg,.png,.jpeg";

Option 2:

Use JS to obtain the suffix name of the user's selected file and verify that it is consistent with the requirements

function
{ var fl = document.getElementById ("FileUpload1"); var aa = Fl.value.substr (fl.value.length-4, 4); if (AA! = ". txt" && aa! = ". png" && AA! = ". jpg"
{ alert ("file format selection is incorrect!) "); return false ; }};

Question 4: file too large

Method 1, expansion

Web. config configuration file:
In kilobytes, default 4096, not too large because it consumes server memory

Method 2, File size limit
1. C # End Limit
if (FileUpload1.PostedFile.ContentLength > (4096 * 1024)) However, it is not useful

2. Client JS limit
Fl.files[0].size can get the size of the selected file, B is the unit

function () {        var fl = document.getElementById ("FileUpload1");         if (Fl.files[0].size > (1024x768 * 4)) {            alert ("file exceeds 4MB");             return false ;        }};

Chapter 6. File Upload

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.