asp.net implementation file No Refresh Upload Method Summary _ Practical skills

Source: Internet
Author: User

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

//uploadify Initialize $ (function () {$ (' #file_upload '). Uploadify ({//Specify SWF
        ' swf ': '/uploadify/uploadify.swf ',//server-side handler ' 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 (' not on
      Transfer of authority ');
      } if (data = = ' Error ') {alert (' upload failed ');
        else if (response) {alert (' Upload success ~ ~ ~ ~ ');
      $ ("#filePath"). val (data); }} uploadify 
<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 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 pictures to 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 {false;
  }} public static class FileTypes {private static list<string> allowedfiletypes = new list<string> ();
  
  Gets the Allow JSON configuration file private static string Jsonfilepath = Common.PathHelper.MapPath ("~/allowedfiletypes.json");
    <summary>///allowed file types///</summary> public static list<string> Allowedfiletypes {get
    {return 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///</summary> public static void Filetypestojson () {When adding allowed file types
    String types = Common.ConverterHelper.ObjectToJson (allowedfiletypes);
  File.writealltext (Jsonfilepath, types); ///<summary>///New allow upload file extension///</summary>///<param name= "Newfiletype" ></param> p
    ublic static void Addnewfiletype (String newfiletype) {Allowedfiletypes.add (newfiletype);
  Filetypestojson (); ///<summary>///Determine if a file type is allowed to upload///</summary>///<param name= "Fileext" > file name extension &LT;/PARAM&G
  T <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;
  return false; }
}

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

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.