asp.net implementation file No Refresh Upload method summary

Source: Internet
Author: User
Tags bool current time json static class

This article is to introduce you to the ASP.net implementation of the file no refresh upload 2 methods, is the use of SWFUpload plug-ins and Uploadify Plug-ins, told a very detailed and comprehensive, accompanied by examples, the need for small partners can refer to.

Encountered the problem of uploading files, combined with the previous use of the SWFUpload, and found a no refresh upload file of the jquery plugin uploadify, write a blog record to introduce the two implementation methods

SWFUpload Import swfupload Development Package Add JS Reference, reference swfupload.js and Handler.js file, if the SWFUpload do not understand, have questions to see this article page initialization

To modify the successful upload event in the Handler.js file, Serverdata is the server-side response

Uploadify Import uploadify Development package, download from the official website, official website documents, Chinese documents, official website example add JS and CSS references, jquery.uploadify.js, uploadify.css

(Note: The path to referencing a uploadify-cancel.png picture file in CSS may be incorrect and can be changed in the Uploadify.css file)

Page initialization

When the page is initialized, you can specify many settings and overload the uploaded events, data representing the server-side response

Server-side Upload Handler

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28 Uploadify initializes $ (function () {$ (' #file_upload '). Uploadify ({///Specify SWF ' swf ': '/uploadify/uploadify.swf ',//server-side handlers ') Uploader ': '/admin/uploadfilehandler.ashx ',//Button text ButtonText: ' Upload attachment ',//file type filetypeexts: "*.zip;*.rar;*.doc;*.docx ; *.xls;*xlsx ", onuploadsuccess:onfileuploadsuccess}); }); function onfileuploadsuccess (file, data, response) {//server-side response if (data = = ' nopermission ') {alert (' no upload permission ');} if (data = =   ' Error '} {alert (' upload failed ');} else if (response) {alert (' Upload success ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~] $ (' #filePath '). val (data)}} Uploadify

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 112 113 114 116 117 118 119 121 122 123 124-126-127 128 129 <summary>///upload file///</summary> public class Uploadfilehandler:ihttphandler, IRequiresSessionState { public void ProcessRequest (HttpContext context) {context. Response.ContentType = "Text/plain"; Verify upload permission if (context). session["User"] = = NULL) {context. Response.Write ("no permission"); Context. Response.End (); Return try {//Get upload file//filedata is defined by the client and if you want to change it, change the configuration in the js file httppostedfile image_upload = context. request.files["Filedata"]; Gets the file name extension string fileext = System.IO.Path.GetExtension (image_upload. FileName). ToLower (); Verify that the file name extension meets the requirements, is the allowed picture format if (! Filetypes.isallowed (Fileext)) {return;}//Current time string, string timestring = DateTime.Now.ToString ("Yyyymmddhhmmssfff"); Save virtual path Build string path = "/upload/" + timestring + fileext; Gets, constructs the physical path to upload the file string serverpath = context. Server.MapPath ("~/" + path); Save the picture to the server image_upload. SaveAs (Serverpath); The output saves the path context. Response.Write (path); The catch (Exception ex) {context. Response.Write ("Error"); Log New Common.loghelper (typeof (Uploadfilehandler)). Error (ex); }   public bool IsReusable {get {return false;}}} public static class FileTypes {private static list<string> allowedfiletypes = new list<string> ()//Get allow JSON to match Set file private static string Jsonfilepath = Common.PathHelper.MapPath ("~/allowedfiletypes.json");  ///<summary>///allowed file types///</summary> public static list<string> Allowedfiletypes {get {Retu RN Allowedfiletypes; }   Set {allowedfiletypes = value;}}  ///<summary>///static construction method///</summary> static filetypes () {Loadfiletypesfromjson ();}  ///< Summary>///reads the file type that is allowed to be uploaded from the JSON file///</summary> private static void Loadfiletypesfromjson () {String types = File . ReadAllText (Jsonfilepath); Allowedfiletypes = common.converterhelper.jsontoobject<list<string>> (types); }  ///<summary>///Update to JSON file when adding allowed file types///</summary> public static void Filetypestojson () {string T Ypes = CommoN.converterhelper.objecttojson (allowedfiletypes); File.writealltext (Jsonfilepath, types);  ///<summary>///new allow upload file extension///</summary>///<param name= "Newfiletype" ></param> Publ IC static void Addnewfiletype (String newfiletype) {Allowedfiletypes.add (newfiletype); Filetypestojson (); }  ///<summary>///to determine whether a file type is allowed to be uploaded///</summary>///<param name= "Fileext" > file name extension </param> <returns> whether to allow upload <code>true</code> allow upload </returns> public static bool Isallowed (string Fileext) {foreach (string item in Allowedfiletypes) {if (Fileext.equals (Fileext)) {return true;}}

The above is the entire contents of this article, I hope you can enjoy.

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.