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.