Upload files using jquery

Source: Internet
Author: User

Http://hi.baidu.com/shouxin1014/blog/item/43aa70504fb6f6998c5430b5.html

Client code:

<Link href = "uploadjs/jquery. uploadify-v2.1.4/uploadify.css" rel = "stylesheet" type = "text/CSS"/>

<SCRIPT type = "text/JavaScript" src = "uploadjs/jquery. uploadify-v2.1.4/jquery-1.4.2.min.js"> </SCRIPT>

<SCRIPT type = "text/JavaScript" src = "uploadjs/jquery. uploadify-v2.1.4/swfobject. js"> </SCRIPT>

<SCRIPT type = "text/JavaScript" src = "uploadjs/jquery. uploadify-v2.1.4/jquery. uploadify. v2.1.4.min. js"> </SCRIPT>

<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function ()
{
$ ("# Uploadify"). uploadify ({
'Upload': '/uploadjs/jquery. uploadify-v2.1.4/uploadify.swf ',
'Script': '/uploadjs/uploadfile. ashx', // backend Processing
'Cancelim': '/uploadjs/jquery. uploadify-v2.1.4/cancel.png', // single cancel button
'Folder': '/uploadfile', // folder
'Queue id': 'filequeue ', // file queue
'Auto': false,
'Multi ': True, // Multifile upload,
'Simuploadlimit ': '3', // number of files that can be uploaded by multiple files
'Text': '*. Doc; *. pdf; *. rar; *. Zip; *. 7z; *. chm', // .jpg, .bmp, .gif, .png, .doc,.htm, .rar,.zip,.7z,
'Filedesc': '*. Doc; *. pdf; *. rar; *. Zip; *. 7z; *. chm ',
'Buttontext': 'select ',
'Buttonimg ':'/images/gerennav2.gif ',
'Width': '123 ',
'Height': '32 ',
'Sizelimmit ': '000000', // size limit, 6 m
Onerror: function (event, queueid, fileobj)
{
Alert ('error' + D. Type + ":" + d.info );
},
Oncomplete: function (event, queueid, fileobj, response, data)
{
// Alert ("successful upload, average rate:" + data. Speed + "kb/s ");
If (response = "0 ")
$ ("# Haveupfile"). append ("<P style = 'color: # f00'> upload" + fileobj. Name + "failed! <A onclick = 'deletefile (\ "\", this) '> Delete </a> </P> ");
Else if (response = "1 ")
$ ("# Haveupfile"). append ("<P style = 'color: # f00'>" + fileobj. Name + "File Format error! <A onclick = 'deletefile (\ "\", this) '> Delete </a> </P> ");
Else
$ ("# Haveupfile"). append ("<p> upload <B>" + response + "</B> successful! <A onclick = 'deletefile (\ "" + response + "\", this) 'style = 'cursor: pointer; '> Delete </a> </P> ");
},
// Onallcomplete: function (E, D)
//{
/// Alert ("successfully uploaded" + D. filesuploaded + "files, error:" + D. Errors );
//},
Onselect: function (E, Q, F)
{
If (F. size> 5242880)
{
Alert ("the file size exceeds the limit! Please reselect ");
Uploadifycancel (Q );
}
}
});
});
Function deletefile (URL, OBJ ){
If (URL! = "")
{
$. Post ("/uploadjs/delfile. ashx", {"fileurl": URL}, function (_ result ){
If (_ result = "1") Alert ("deleted successfully! ");
Else if (_ result = "2") Alert ("deletion exception! ");
Else if (_ result = "3") Alert ("request failed! ");
Else if (_ result = "4") Alert ("this file does not exist! ");
$ (OBJ). Parent (). Remove ();
});
}
Else
$ (OBJ). Parent (). Remove ();
}
</SCRIPT>

HTML:

<Div id = "filequeue">
</Div>
<Input type = "file" name = "uploadify" id = "uploadify"/>
<P>
<A href = "javascript: $ ('# uploadify '). uploadifyupload () "> upload </a> | <a href =" javascript: $ ('# uploadify '). uploadifyclearqueue () ">
Cancel upload </a>
</P>
<Div id = "haveupfile"> </div>

Upload Background:

<% @ Webhandler Language = "C #" class = "uploadfile" %>

Using system;
Using system. Web;
Using system. IO;
Public class uploadfile: ihttphandler
{
Public String extensionlimit = ".jpg, .bmp, .gif,.png, .doc,.htm, .rar,.zip,.7z,"; // separated by commas (,), in lower case ..
Public void processrequest (httpcontext context)
{
Context. response. contenttype = "text/plain ";
Context. response. charset = "UTF-8 ";

Httppostedfile file = context. Request. Files ["filedata"];
String thepath = @ context. request ["folder"];
String uploadpath = httpcontext. Current. server. mappath (thepath );

If (file! = NULL)
{
If (extensionlimit. indexof (system. Io. Path. getextension (file. filename). tolower () + ",")>-1)
{
Thepath + = "\" + datetime. Now. year. tostring () + "-" + datetime. Now. Month. tostring ();
Uploadpath + = "\" + datetime. Now. year. tostring () + "-" + datetime. Now. Month. tostring ();
If (! Directory. exists (uploadpath ))
{
Directory. createdirectory (uploadpath );
}
String refile = "OA" + datetime. now. day. tostring () + datetime. now. hour. tostring () + datetime. now. minute. tostring () + "-"; // prefix file name
File. saveas (uploadpath + "\" + refile + file. filename );
// If the following code is missing, the display of the upload queue will not disappear automatically after the upload is successful.
// Return the network path
Thepath + = "\" + refile + file. filename;
Thepath = thepath. Replace ("\\","/");
Context. response. Write (thepath );
}
Else
{
Context. response. Write ("1"); // extension Verification Failed
}
}
Else
{
Context. response. Write ("0"); // Verification Failed
}
}

Public bool isreusable
{
Get
{
Return false;
}
}

}

Delete Background:

<% @ Webhandler Language = "C #" class = "delfile" %>

Using system;
Using system. Web;
Using system. IO;
Public class delfile: ihttphandler {

Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
Context. response. charset = "UTF-8 ";
If (context. Request. Form ["fileurl"]! = NULL)
{
String thefile = httpcontext. Current. server. mappath (context. Request. Form ["fileurl"]);
If (file. exists (thefile ))
{
Try
{
File. Delete (thefile );
Context. response. Write ("1 ");
}
Catch {context. response. Write ("2 ");}
}
Else
{
Context. response. Write ("4 ");
}
}
Else
{
Context. response. Write ("3 ");
}
}
 
Public bool isreusable {
Get {
Return false;
}
}

}

Project path

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.