Use JS Javascript in ASP. NET to get the fileupload File Size

Source: Internet
Author: User

In the background Code, although filecontent. length can be used to obtain the size, this method should be avoided as much as possible.

The server-side code will run on the server side, so the verification using this method will find it very slow. Because the page first uploads the file to the temporary space on the server, and the server obtains the file size. Therefore, the efficiency is very low, which seriously occupies the bandwidth. Method is not available. In particular, when the network speed is poor, a prompt is displayed after the customer uploads the file for a long time. It is strange not to crash.

Therefore, JS Code is preferred.

The following is the text:

First, add a fileupload control on the page. (Taking the ID fudpolicy as an example again)

Then add an upload button. Sample interface.

 

<Script language = "JavaScript" type = "text/JavaScript">

// Verify the file size

Function ckfilesize ()

{

// Pre-load the attachment as an image processing result in the buffer zone

Var file = new image ();

// Set the URL of the Attachment

File. DYNSRC = Document. getelementbyid ("fudpolicy"). value;

// Obtain the size of the uploaded file

VaR filesize = file. filesize/1024;

If (filesize> 3072)

{

Alert ("the uploaded file cannot be larger than m ");

Return false;

}

Return true;

}

</SCRIPT>

Finally, you can call it in the onload event.

IE6 passed the test, and IE7 refused to access the service.

The following code can be used in IE7:

  1. <SCRIPT type = "text/JavaScript">
  2. FunctionGetfilesize (filepath)
  3. {
  4. VaRFSO =NewActivexobject ("scripting. FileSystemObject ");
  5. Alert ("file size:" + FSO. GetFile (filepath). size );
  6. }
  7. </SCRIPT>
  8. <Body>
  9. <Input type = "file" name = "file" size = "30" onchange = "getfilesize (this. Value);">
  10. </Body>

This method can be implemented and easily thought by developers, but the only drawback is that there is a security prompt. Of course, change the file name. HTA will block the security prompt, but it is difficult to obtain it.Not recommended, not to mention

<Form ID = "form1" runat = "server" onsubmit = "Return ckfilesize ()">


Next, let's talk about another method. There is an IMG tag in the HTML Tag that is not "deep" for general developers. let's first talk about its attributes: SRC, DYNSRC, start, ALT, controls, loop, loopdelay, hspace, vspace .... some commonly used attributes will not be listed. Here we will talk about the "DYNSRC" attribute: DYNSRC can be used to insert various multimedia, the format can be WAV, Avi, AIFF, AU, MP3, Ra, ram, and so on. The URL is an audio or video file and its path, which can be a relative or absolute path.

Example:

In this way, the path of any type of file can be dynamically assigned based on DYNSRC. In JavaScript, the file size can be obtained based on The filesize attribute of the image object. Of course, the image object has several other attributes, such as filecreateddate, filemodifieddate, filesize, fileupdateddate, and filters.... The Code is as follows:

JavaScript code
<SCRIPT type = "text/JavaScript">
Function getfilesize (filepath)
{
VaR image = new image ();
Image. DYNSRC = filepath;
Alert (image. filesize );
}
</SCRIPT>
<Body>
<Input type = "file" name = "file" size = "30" onchange = "getfilesize (this)">
</Body>
<SCRIPT type = "text/JavaScript">
Function getfilesize (fileobj)
{
VaR image = new image ();
Image. DYNSRC = fileobj. value;
Alert (image. filesize | fileobj. Files [0]. filesize );
}
</SCRIPT>
<Body>
<Input type = "file" name = "file" size = "30" onchange = "getfilesize (this. Value)">
</Body>

JavaScript to determine the file type

VaR type = (SRC. substr (SRC. lastindexof ("."). tolowercase ();
If (type! = ". Jpg" & type! = ". GIF" & type! = ". Jpeg" & type! = ". PNG") {alert ("the image you upload does not match (.jpg |. JPEG |. gif |. PNG )! "); Return false ;}

Related Article

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.