c#-webform-File Upload-fileupload control

Source: Internet
Author: User

//< upload > button    voidButton1_Click (Objectsender, EventArgs e) {        //determine if the file is selected        if(FileUpload1.FileName.Length <=0) {Label1.Text="no files are selected! "; return; }        //< upload > function code//------------------------------------------------------        //new folder uploads, setting save file name and type Abc.txt        stringPath ="Uploads/abc.txt"; //Map Absolute Path (Server.MapPath ())        stringEndpath = Server.MapPath (path);//Mapping Absolute Paths//Show upload Save folder absolute pathLabel1.Text =Endpath; //Save to Server (SaveAs ())Fileupload1.saveas (Endpath);//Save to Absolute path//------------------------------------------------------}

1, how to determine whether to select the file?
Fileupload.filename-
The file name of the selected file, if the length is not greater than 0, then no file is selected
Js-f.value.length

2, how to save to the server?
Fileupload.saveas ("absolute path");

3. How to get the absolute path? (Lee Brainstorming Lxc)
Write the relative path first-"Uploads/abc.txt"
Mapping relative paths to absolute paths-Server.MapPath ("Uploads/abc.txt");

-----------------------------------------------------------------------------

In the above method, only the. txt file can be uploaded, and the name can only be ABC. How do I keep the original name and file type?

// new Folder uploads, set save file name + selected file name

string path = "uploads/"+fileupload1.filename;

The same file will be overwritten. How do I keep the file's uniqueness from being overwritten?

Before file name + login Username + current time (accurate to milliseconds)

// new folder uploads, setting save file name        string " uploads/ " + request.cookies["username"]. Value + DateTime.Now.ToString ("yyyymmddhhmmssms") + fileupload1.filename;

-----------------------------------------------------------------------------

4, now can only be uploaded into TXT file, and the name can only be ABC
How do I keep a file's original name and file type?
"uploads/" + fileupload1.filename;

5, how to prevent the problem of duplicate name coverage?
"uploads/" + request.cookies["user"]. Value + DateTime.Now.ToString ("yyyymmddhhmmssms") + fileupload1.filename;

-----------------------------------------------------------------------------

How do I restrict the type of selected files? (Lee Brainstorming Lxc)

Front end FileUpload The type that is displayed when you select a file:

<asp:fileupload id="FileUpload1" runat="server" accept= " . Jpg,.png,.jpeg,.txt " />

The user can choose "All Files" publicly to select other types of files, how to limit?

JS Code:

<script type="Text/javascript">//< upload > button click events (JS priority highest)document.getElementById ("Button1"). onclick =function () {//Gets the selected file name        varfilename = document.getElementById ("FileUpload1"). Value; //4-bit or 5-bit after intercepting file name        varn = filename.substr (Filename.length-4,4); varm = Filename.substr (Filename.length-5,5); //determine if n or M does not meet the condition to terminate execution, otherwise continue        if(N!==". txt"&& N!==". jpg"&& N!==". PNG"&& m!==". JPEG") {alert ("Please choose the correct file format! "); return false; }    };</script>

-----------------------------------------------------------------------------

6. How to restrict the type of selected files?
Restrict ordinary people, add properties to the control-accept= ". Jpg,.png,.jpeg,.txt"
Limit the trouble:
document.getElementById ("Button1"). onclick = function () {
var fileName = document.getElementById ("FileUpload1"). Value;

var name = Filename.substr (filename.length-4, 4);
var name1 = Filename.substr (filename.length-5, 5);
if (name! = ". jpg" && name! = ". png" && name! = ". txt" && name1! = ". jpeg")
{
Alert ("Please select the correct file!") You're looking for something, huh? "); (Lee brainstorming Lxc)
return false;
}
};

-----------------------------------------------------------------------------

system default maximum upload size is 4M, if the file is too large to upload

How can I limit the size of my uploaded files? Expansion or limit size (Li brainstorming lxc)

Expansion:

In the Webconfig configuration file:

<configuration>  <system.web>    <compilation debug="false" targetframework="4.0" />    <!--set maximum upload file size 40M, Unit kb-->    "40960" />  </system.web></configuration>

Attention! Do not expand too much, or many people at the same time uploading large files will cause server memory shortage!

Limit size:

//< upload > button    voidButton1_Click (Objectsender, EventArgs e) {        //determine if the file is selected        if(FileUpload1.FileName.Length <=0) {Label1.Text="no files are selected! "; return; }        //determines if the length of the selected file is greater than 4M (the default is B)        if(FileUpload1.PostedFile.ContentLength >4*1024x768*1024x768) {Label1.Text="file length is too long!! "; return; }        //< upload > function code//------------------------------------------------------        //new folder uploads, setting save file name        stringPath ="uploads/"+ DateTime.Now.ToString ("yyyymmddhhmmssms") +Fileupload1.filename; //Map Absolute Path (Server.MapPath ())        stringEndpath = Server.MapPath (path);//Mapping Absolute Paths//Show upload Save folder absolute pathLabel1.Text =Endpath; //Save to Server (SaveAs ())Fileupload1.saveas (Endpath);//Save to Absolute path//------------------------------------------------------}

Disadvantage:< upload > Click, the System is first submitted to the background, and then judged, submitted to the background speed affected

JS End limit:

<script type="Text/javascript">//< upload > button click events (JS priority highest)document.getElementById ("Button1"). onclick =function () {//determine the length of the file to be uploaded        varf = document.getElementById ("FileUpload1"); if(f.files[0].size >4*1024x768*1024x768) {Label1.Text= Alert ("file length is too long!! "); return; }        //Gets the selected file name        varfilename = document.getElementById ("FileUpload1"). Value; //4-bit or 5-bit after intercepting file name        varn = filename.substr (Filename.length-4,4); varm = Filename.substr (Filename.length-5,5); //determine if n or M does not meet the condition to terminate execution, otherwise continue        if(N!==". txt"&& N!==". jpg"&& N!==". PNG"&& m!==". JPEG") {alert ("Please choose the correct file format! "); return false; }    };</script>

Advantage: The first judgment and then submit the upload, do not occupy the upload memory.

Note: C # side and JS end to write, in order to prevent bypass JS side directly upload

-----------------------------------------------------------------------------

7, control the size of the upload file
Expansion-system default allowed maximum upload length is 4MB
Webconfig in the configuration file
<system.web>
<compilation debug= "false" targetframework= "4.0"/>
</system.web>

Baidu Search, research, C # large file upload, breakpoint continued to pass.

Attention! Do not expand too much, or many people at the same time uploading large files will cause server memory shortage!

Limit size
C # End Limit:
if (fileupload1.postedfile.contentlength> (4*1024*1024))
{
Label1.Text = "File length too long!!!" ";
Return
}

JS End limit:
var f = document.getElementById ("FileUpload1");

if (F.files[0].size > (4 * 1024 * 1024)) {
Alert ("File Too big!!! ");
return false;
}

c#-webform-File Upload-fileupload control

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.