您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> asp.net编程 >> 正文

asp.net实现文件无刷新上传方法汇总

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   本文给大家介绍的是asp.net实现文件无刷新上传的2种方法,分别是使用swfupload插件和uploadify插件,讲述的十分细致全面,附上示例,有需要的小伙伴可以参考下。

  遇到上传文件的问题,结合之前用到过的swfUpload,又找了一个无刷新上传文件的jquery插件uploadify,写篇博客记录一下分别介绍这两个的实现方法

  swfUpload 导入swfUpload的开发包 添加js引用,引用swfUpload.js与handler.js文件,如果对swfUpload不了解、有疑问可以看看这篇文章 页面初始化

  修改handler.js文件中 上传成功的事件,serverData是服务器端的响应

  Uploadify 导入uploadify开发包,从官网下载,官网文档,中文文档,官网示例 添加js与css的引用,jquery.uploadify.js 、uploadify.css

  (注:在css中引用uploadify-cancel.png图片文件的路径是可能不正确,可以在uploadify.css文件中自己进行更改)

  页面初始化

  页面初始化时,可以指定许多设置,并对上传成功的事件进行重载,data表示服务器端的响应

  服务器端上传处理程序

  ?

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初始化 $(function () { $('#file_upload').uploadify({ //指定swf 'swf': '/uploadify/uploadify.swf', //服务器端处理程序 'uploader': '/Admin/UploadFileHandler.ashx', //按钮文本 buttonText: '上传附件', //文件类型 fileTypeExts: "*.zip;*.rar;*.doc;*.docx;*.xls;*xlsx", onUploadSuccess: OnFileUploadSuccess }); }); function OnFileUploadSuccess(file, data, response) { //服务器端响应 if (data == 'noPermission') { alert('没有上传权限'); } if (data == 'Error') { alert('上传失败'); } else if (response) { alert('上传成功~~~'); $("#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 32 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 /// <summary> /// 上传文件 /// </summary> public class UploadFileHandler : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //验证上传权限 if (context.Session["User"] == null) { context.Response.Write("no permission"); context.Response.End(); return; } try { //获取上传文件 //Filedata是客户端已经定义好的,如果想要更改,更改js文件中的配置 HttpPostedFile image_upload = context.Request.Files["Filedata"]; //获取文件扩展名 string fileExt = System.IO.Path.GetExtension(image_upload.FileName).ToLower(); //验证文件扩展名是否符合要求,是否是允许的图片格式 if (!FileTypes.IsAllowed(fileExt)) { return; } //当前时间字符串 string timeString = DateTime.Now.ToString("yyyyMMddHHmmssfff"); //保存虚拟路径构建 string path = "/Upload/" + timeString + fileExt; //获取、构建要上传文件的物理路径 string serverPath = context.Server.MapPath("~/" + path); //保存图片到服务器 image_upload.SaveAs(serverPath); //输出保存路径 context.Response.Write(path); } catch (Exception ex) { context.Response.Write("Error"); //记录日志 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>(); //获取允许json配置文件 private static string jsonFilePath = Common.PathHelper.MapPath("~/AllowedFileTypes.json");   /// <summary> /// 允许的文件类型 /// </summary> public static List<string> AllowedFileTypes { get { return allowedFileTypes; }   set { allowedFileTypes = value; } }