Lightweight, pseudo-Ajax file upload.

Source: Internet
Author: User

JS file:

 

 

 

Document. writeln ("<Div id = 'answerupfilediv 'style = 'text-align: right'> </div> ");
VaR uploaddemo = "<input type = 'den den 'value ='' id = 'upfilename' name = 'upfilename'/> <Form ID = 'formfile' name = 'formfile' Method = \ "Post \" Action = 'HTTP: // www.cnblogs.com/upload.aspx'target = 'framefile' enctype = \ "multipart/form-data \"> <input type = 'file' id = 'fileup' onchange = 'uploadfilechange (); 'name = 'fileup'/> </form> <IFRAME id = 'framefile' name = 'framefile' style = 'display: none; '> </iframe> <Div style = 'display: none' id = 'uploadlog'> </div> <Div id = 'uploadfilelinkdiv' style = 'display: none '> <a id = 'uploadfilehrefa' href = '.. /upfile/'target = '_ blank'> </a> & nbsp; <a style = 'cursor: pointer; display: none; color: # 018dca 'Id = 'updateuploadfilelink' onclick = 'updateuploadfile (); '> Update </a> </div>"

$ ("# Answerupfilediv"). append (uploaddemo );

 

// $ (Function (){
// $ ('# Fileup'). Change (function (){
// Encode ('uploadlog'upload.html ('loading ....');
// $ ('# Formfile'). Submit ();
//});
//})

 

//. If there is a login restriction and the chain has an onclick, event delegation cannot be used.

 

Function uploadfilechange (){
Certificate ('uploadfilelinkdiv'mirror.css ('display', 'None ');
Upload ('uploadlog'{.html ('loading ....');
Certificate ('uploadlog'demo.css ('display ','');
$ ('# Formfile'). Submit ();
}

 

 

 

Function uploadsuccess (MSG ){
If (msg. Split ('|'). length> 1 ){
$ ('# Upfilename'). Val (msg. Split ('|') [1]);
Certificate ('{uploadlog'{.html ('');
Certificate ('uploadlog'demo.css ('display', 'None ');
Certificate ('externformfile').css ('display', 'None ');
$ ('# Answeruploadfilebutton'). ATTR ('Disabled ', true );
Certificate ('{uploadfilehrefa'}.html (msg. Split ('|') [1]). Split ('/') [1]);
$ ('# Uploadfilehrefa'). ATTR ('href ', upfilebasepath + MSG. Split (' | ') [1]);
Certificate ('updateuploadfilelink'demo.css ('display ','');
Certificate ('uploadfilelinkdiv'mirror.css ('display ','');
} Else {
Upload ('uploadlog'apps.html (MSG );

}
}

 

 

// Modified and added the upload attachment.

 

 

Function updateuploadfile (){
Certificate ('your formfile').css ('display ','');
$ ('# Answeruploadfilebutton'). removeattr ('Disabled ');
}

 

 

 

// Added Logon Restrictions $ ("# Fileup"). BIND ("click", function (){
If (isvaluserlogin = 0 &&! Valuserlgoin ($ ("# answerupfilediv "))){
Showfadediv ($ ("# lgdiv"), $ ("# answerupfilediv "));
Return false;
}
})

 

 

 

 

 //, Compatible with Firefox and IE. Previous versions can only be used in Firefox,

 

Xxx. aspx:

 

<Div>

<! -- Display the upload location -->

 

<SCRIPT src = "./JS/jquery-1.4.2.min.js" type = "text/JavaScript"> </SCRIPT>

 

 

<SCRIPT src = "../JS/uploadfile. js" type = "text/JavaScript"> </SCRIPT>

 

</Div>

 

 

 

 

 

Upload. aspx. CS :( must drop )

 

 

 

Protected void page_load (Object sender, eventargs E)
{
Try
{
// Get the file set object from the current post. Here I only get the file control of <input type = 'file' name = 'fileup'/>.
Httppostedfile file = request. Files ["fileup"];
If (file! = NULL)
{
// Directory to which the current file is uploaded
String Path = server. mappath ("~ /");
// The path of the server to be uploaded
String imageurl = path + path. getfilename (file. filename );
// Suffix of the current file
String ext = path. getextension (file. filename). tolower ();
// Verify that the file type is correct
If (! Ext. Equals (". GIF ")&&! Ext. Equals (". jpg ")&&! Ext. Equals (". PNG ")&&! Ext. Equals (". BMP "))
{
// Here window. Parent. uploadsuccess () is the JavaScript function written on the front-end page. This method is mainly used to output exceptions and the image address after the upload is successful.
Response. Write ("<SCRIPT> window. Parent. uploadsuccess ('the file you uploaded is incorrectly formatted! The upload format is (.gif、.jpg?.png=.bmp) '); </SCRIPT> ");
}
// Verify the file size
If (file. contentlength> 1048576)
{
// Here window. Parent. uploadsuccess () is the JavaScript function written on the front-end page. This method is mainly used to output exceptions and the image address after the upload is successful.
Response. Write ("<SCRIPT> window. Parent. uploadsuccess ('the file you uploaded cannot exceed 1048576kb! Upload again! '); </SCRIPT> ");
}
// Start upload
File. saveas (imageurl );

// Here window. Parent. uploadsuccess () is the JavaScript function written on the front-end page. This method is mainly used to output exceptions and the image address after the upload is successful.
// If the data returned successfully requires two strings to be returned, I have used | separator example: Success information |/test/hello.jpg
Response. Write ("<SCRIPT> window. Parent. uploadsuccess ('upload success! |/Test/"+ file. filename +" '); </SCRIPT> ");
}
Else
{
// Upload Failed
Response. Write ("Upload lose! ");
}
}
Catch
{
// Upload Failed
Response. Write ("Upload lose! ");
}
}

 

 

 

 

 

 It is said that people who do not reply often play debug

 

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.