Asp.net implements a Summary of the methods for uploading files without refreshing, and asp.net uploads files.

Source: Internet
Author: User

Asp.net implements a Summary of the methods for uploading files without refreshing, and asp.net uploads files.

When uploading files, combined with the previously used swfUpload, I found a jquery plug-in uploadify for refreshing new files. I wrote a blog record to introduce the implementation methods of the two.

Add js references to the swfUpload import SDK and reference the swfUpload. js and handler. js files. If you are not familiar with swfUpload and have any questions, please refer to this article page for initialization.

Modify the successful upload event in the handler. js file. serverData is the server response.

Uploadify import uploadify Development Kit, download from the official website, official website documents, Chinese documents, official website examples Add references for js and css, jquery. uploadify. js unzip uploadify.css

(Note: In css, the upload of the uploadify-cancel.pngimage file is optional. You can change it in the uploadify.css file)

Page Initialization

During page initialization, you can specify multiple settings and reload uploaded events. data indicates the server response.

Server-side upload Handler

// Uploadify initialization $ (function () {$ ('# file_upload '). uploadify ({// specify the swf 'swf ':'/uploadify/uploadify.swf ', // the server-side handler 'upload':'/Admin/UploadFileHandler. ashx', // button text buttonText: 'upload attachment ', // file type fileTypeExts :"*. zip ;*. rar ;*. doc ;*. docx ;*. xls; * xlsx ", onUploadSuccess: OnFileUploadSuccess}); function OnFileUploadSuccess (file, data, response) {// if (data = 'nopermission ') {alert ('no upload authorization '); } If (data = 'error') {alert ('upload failed');} else if (response) {alert ('upload successful ~~~ '); $ ("# FilePath"). val (data) ;}} uploadify
/// <Summary> // upload a file /// </summary> public class UploadFileHandler: IHttpHandler, IRequiresSessionState {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; // verify the upload permission if (context. session ["User"] = null) {context. response. write ("no permission"); context. response. end (); return;} try {// obtain the uploaded file // Filedata is defined by the client. If you want to change it, change the configuration in the js file HttpPostedFile image_upl. Oad = context. request. files ["Filedata"]; // obtain the file extension string fileExt = System. IO. path. getExtension (image_upload.FileName ). toLower (); // verify whether the file extension meets the requirements and whether the image format is allowed if (! FileTypes. isAllowed (fileExt) {return;} // current time string timeString = DateTime. now. toString ("yyyyMMddHHmmssfff"); // Save the virtual path to build string path = "/Upload/" + timeString + fileExt; // obtain and construct the physical path of the file to be uploaded string serverPath = context. server. mapPath ("~ /"+ Path); // Save the image to the server image_upload.SaveAs (serverPath); // output the Save path context. response. write (path);} catch (Exception ex) {context. response. write ("Error"); // record the 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> (); // obtain the allowed json configuration file private static s Tring jsonFilePath = Common. PathHelper. MapPath ("~ /AllowedFileTypes. json "); // <summary> /// allowed file types // </summary> public static List <string> AllowedFileTypes {get {return allowedFileTypes ;} set {allowedFileTypes = value ;}/// <summary> // static constructor /// </summary> static FileTypes () {LoadFileTypesFromJson ();} /// <summary> /// read the File type that can be uploaded from the json File /// </summary> private static void LoadFileTypesFromJson () {string types = File. readAllText (jsonFilePath); AllowedFileTypes = Common. converterHelper. jsonToObject <List <string> (types) ;}/// <summary> // when the file type is allowed, update to the json file // </summary> public static void FileTypesToJson () {string types = Common. converterHelper. objectToJson (AllowedFileTypes); File. writeAllText (jsonFilePath, types );} /// <summary> /// Add a file extension that can be uploaded /// </summary> /// <param name = "newFileType"> </param> public static void AddNewFileType (string newFileType) {AllowedFileTypes. add (newFileType); FileTypesToJson ();} /// <summary> /// determine whether a certain file type is allowed to be uploaded // </summary> /// <param name = "fileExt"> file extension </param> /// <returns> whether to allow upload <code> true </code> </returns> public static bool IsAllowed (string fileExt) {foreach (string item in AllowedFileTypes) {if (fileExt. equals (fileExt) {return true ;}} return false ;}}

The above is all the content of this article. I hope you will like it.

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.