Asp. NET plugin uploadify batch upload file Full use tutorial _ Practical skills

Source: Internet
Author: User
Tags datetime hash httpcontext urlencode

uploadify Batch upload file complete use of the tutorial for your reference, the specific content as follows

1. First prepare uploadify js file, a lot of online search

2. Upload page upfilepage.aspx

Key code:

 

GetFile () method in the background:

<summary>///Get cookie attachment information///</summary>///<returns></returns> protected string GetFile ()
{#region Get cookie attachment information StringBuilder strhtml = new StringBuilder ();
HttpCookie Filecookie = request.cookies["Filecookie"]; if (Filecookie!= null) {string[] Filearray = new string[1]; if (FileCookie.Value.Contains ("|")) Filearray = filecookie.v Alue.
Split (' | ');
else filearray[0] = Filecookie.value; foreach (String objfile in Filearray) {if!string. IsNullOrEmpty (objfile) && objfile.contains (",")) {string[] file = Objfile.split (', '); Strhtml.append (@ "<
Div class= ' uploadifyqueueitem ' > ');
Strhtml.append (@ "<div class= ' Cancel ' >");
Strhtml.append ("<a href= ' Javascript:deletefile (\" "+ file[1] +" \ ") ' ></a> ');
Strhtml.append (@ "<span class= ' pErcentage ' >-100%</span><div class= ' uploadifyprogress ' > '); Strhtml.append (@ "<div class= ' Uploadifyprogressbar ' style= ' width:100%; ')
> ");
Strhtml.append (@ "</div>");
Strhtml.append (@ "</div>");
Strhtml.append (@ "</div>");
}} return strhtml.tostring ();

 #endregion}

General handlers under 3.UploadAction folders:

public void ProcessRequest (HttpContext context) {context.
Response.ContentType = "Text/plain"; String operate = context.
request["operate"]; if (operate = = "DeleteFile") {#region Delete file attachment information//Get file path string FilePath = Context. Server.MapPath (context.
request["file"]); Determine if the file exists if (file.exists (FilePath)) File.delete (FilePath);//delete file//Get attachment cookie information HttpCookie Filecookie = context.
request.cookies["Filecookie"];
string[] Filearray = new String[1]; if (Filecookie!= null) {FilePath = Filepath.remove (0, Filepath.indexof ("Upfiles")).
Replace ("\", "/");
if (FileCookie.Value.Contains ("|")) Filearray = FileCookie.Value.Split (' | ');
else filearray[0] = Filecookie.value;
String strfile = ""; for (int i = 0; i < filearray.length i++) {if (!filearray[i).
Contains (FilePath)) strfile + = Filearray[i] + "|";
} if (Strfile.contains ("|")) strfile = Strfile.remove (strfile.length-1);
Filecookie.value = strfile;
Filecookie.expires = DateTime.Now.AddDays (1);
Filecookie.httponly = true; Context. Response.apPendcookie (Filecookie);
StringBuilder strhtml = new StringBuilder ();
if (FileCookie.Value.Contains ("|")) Filearray = FileCookie.Value.Split (' | ');
else filearray[0] = Filecookie.value; foreach (String objfile in Filearray) {if!string. IsNullOrEmpty (objfile) && objfile.contains (",")) {string[] file = Objfile.split (', '); Strhtml.append (@ "<
Div class= ' uploadifyqueueitem ' > ');
Strhtml.append (@ "<div class= ' Cancel ' >");
Strhtml.append ("<a href= ' Javascript:deletefile (\" "+ file[1] +" \ ") ' ></a> ');
Strhtml.append (@ "<span class="
Percentage ' >-100%</span><div class= ' uploadifyprogress ' > '); Strhtml.append (@ "<div class= ' Uploadifyprogressbar ' style= ' width:100%; ')
> ");
Strhtml.append (@ "</div>");
Strhtml.append (@ "</div>"); strHTML.
Append (@ "</div>"); }} context.
Response.Write (Strhtml.tostring ());
#endregion} else if (operate = = "GetFile") {#region Get the uploaded attachment and show StringBuilder strhtml = new StringBuilder (); HttpCookie Filecookie = context.
request.cookies["Filecookie"]; if (Filecookie!= null) {string[] Filearray = new string[1]; if (FileCookie.Value.Contains ("|")) Filearray = filecookie.v Alue.
Split (' | ');
else filearray[0] = Filecookie.value; foreach (String objfile in Filearray) {if!string. IsNullOrEmpty (objfile) && objfile.contains (",")) {string[] file = Objfile.split (', '); Strhtml.append (@ "<
Div class= ' uploadifyqueueitem ' > ');
Strhtml.append (@ "<div class= ' Cancel ' >");
Strhtml.append ("<a href= ' Javascript:deletefile (\" "+ file[1] +" \ ") ' > ');
Strhtml.append (@ "</a>");
Strhtml.append (@ "</div>"); Strhtml.append (@ "<span class= ' fileName ' >" + httputility.urldecode (file[0]) + "</span><span class= ' percentage ' >-100%</span><div class= ' uploadifyprogress ' > '); Strhtml.append (@ "<div class= ' Uploadifyprogressbar ' style= ' width:100%; ')
> ");
Strhtml.append (@ "</div>");
Strhtml.append (@ "</div>");
Strhtml.append (@ "</div>"); }} context.
Response.Write (Strhtml.tostring ());

 #endregion}}

4. Upload file UPLOADHANDLER.ASHX General processing program code, file upload path can be set according to the plot needs free:

public void ProcessRequest (HttpContext context) {context.

Response.ContentType = "Text/plain"; HttpCookie Filecookie = context.
request.cookies["Filecookie"]; if (Filecookie!= null) {string[] Filearray = new string[1]; if (FileCookie.Value.Contains ("|")) Filearray = filecookie.v Alue.
Split (' | ');
if (filearray.length >= 5) return;
else {Filecookie = new HttpCookie ("Filecookie"); filecookie.value = ""; Context.
RESPONSE.COOKIES.ADD (Filecookie); String Aspxurl = context. Request.Path.Substring (0, context.

Request.Path.LastIndexOf ("/") + 1);

File save directory path String savepath = "/upfiles/";

File save Directory URL String saveurl = "/upfiles/"; if (context. request.cookies["Member"]!= null)//{//Savepath + = context.
request.cookies["Member" ["MemberID"] + "/"; Saveurl + = context.
request.cookies["Member" ["MemberID"] + "/"; String member = Guid.NewGuid (). ToString (). Trim ().
Replace ("-", "");
Savepath + + "/";

Saveurl + + "/"; Defines the file extensions allowed to be uploaded/*hashtable exttable = new HashtablE ();
Exttable.add ("image", "gif,jpg,jpeg,png,bmp");
Exttable.add ("Flash", "swf,flv");
Exttable.add ("Media", "Swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4"); Exttable.add ("File", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf

, rm,rmvb,mp4,wps "); *///maximum file size int maxSize = 5242880; Httppostedfile Imgfile = context.
request.files["Imgfile"]; /*if (Imgfile = = null) {showerror ("Please select File.") ");
} */String Dirpath = context.
Server.MapPath (Savepath); if (! Directory.Exists (Dirpath)) {directory.createdirectory (Dirpath);//showerror ("Upload directory does not exist.")

");
} String dirname = context.
request.querystring["dir"]; if (String.IsNullOrEmpty (dirname)) {dirname = "file";}/*if (!exttable.containskey (dirname)) {showerror ("directory name is incorrect. ");
}
* * String fileName = imgfile.filename; String Fileext = path.getextension (fileName).


ToLower (); /*if (String.IsNullOrEmpty (fileext) | | Array.indexof ((((String) exttable[dirname]). Split (', '), fileext.substring (1). ToLower ()) = = = 1) {ShoweRror ("The upload file name extension is an disallowed extension.") \ nthe only "+ ((String) exttable[dirname]) +" format is allowed.
");
} if (Dirname.contains ("image") {if (Imgfile.inputstream = null | | imgFile.InputStream.Length > MaxSize) {showerror (" The upload file exceeds the 5M size limit. ");
}
}
*///Create folder Dirpath + + + dirname + "/";
Saveurl + = dirname + "/"; if (!
Directory.Exists (Dirpath)) {directory.createdirectory (Dirpath);}
String ymd = DateTime.Now.ToString ("YyyyMMdd", datetimeformatinfo.invariantinfo);
Dirpath + = Ymd + "/";
Saveurl + = Ymd + "/"; if (!

Directory.Exists (Dirpath)) {directory.createdirectory (Dirpath);}
String NewFileName = DateTime.Now.ToString ("Yyyymmddhhmmss_ffff", datetimeformatinfo.invariantinfo) + Fileext;

String FilePath = Dirpath + newfilename;

Imgfile.saveas (FilePath);

String FILEURL = Saveurl + newfilename;
/*hashtable hash = new Hashtable ();
hash["error"] = 0;
hash["url"] = FILEURL; Context. Response.AddHeader ("Content-type", "text/html;
Charset=utf-8 "); Context.
Response.Write (Jsonmapper.tojson (hash)); Context. Response.End (); */if (fIlecookie!= null) {string strfile = Filecookie.value; if (!string.
IsNullOrEmpty (strfile)) strfile = strfile + "|" + Httputility.urlencode (fileName) + "," + fileUrl;
else strfile = Httputility.urlencode (fileName) + "," + fileUrl;
Filecookie.value = strfile;
Filecookie.expires = DateTime.Now.AddDays (1);
Filecookie.httponly = true; Context.
Response.appendcookie (Filecookie); } context.
Response.Write ("1"); Context.
Response.End ();
 }

5. All code to knock OK, you can reap the results:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.