博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件无刷新上传(swfUpload与uploadify)
阅读量:4308 次
发布时间:2019-06-06

本文共 2269 字,大约阅读时间需要 7 分钟。

 

文件无刷新上传并获取保存到服务器端的路径

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

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

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

  1. Uploadify
  • 导入uploadify开发包,从官网,,,
  • 添加js与css的引用,jquery.uploadify.js 、uploadify.css

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

     

  • 页面初始化

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

  • 服务器端上传处理程序

1     ///   2     /// 上传文件 3 ///  4 public class UploadFileHandler : IHttpHandler, IRequiresSessionState 5 { 6 public void ProcessRequest(HttpContext context) 7 { 8 context.Response.ContentType = "text/plain"; 9 //验证上传权限 10 if (context.Session["User"] == null) 11 { 12 context.Response.Write("no permission"); 13 context.Response.End(); 14 return; 15 } 16 try 17 { 18 //获取上传文件 19 //Filedata是客户端已经定义好的,如果想要更改,更改js文件中的配置 20 HttpPostedFile image_upload = context.Request.Files["Filedata"]; 21 //获取文件扩展名 22 string fileExt = System.IO.Path.GetExtension(image_upload.FileName).ToLower(); 23 //验证文件扩展名是否符合要求,是否是允许的图片格式 24 if (!FileTypes.IsAllowed(fileExt)) 25 { 26 return; 27 } 28 //当前时间字符串 29 string timeString = DateTime.Now.ToString("yyyyMMddHHmmssfff"); 30 //保存虚拟路径构建 31 string path = "/Upload/" + timeString + fileExt; 32 //获取、构建要上传文件的物理路径 33 string serverPath = context.Server.MapPath("~/" + path); 34 //保存图片到服务器 35 image_upload.SaveAs(serverPath); 36 //输出保存路径 37 context.Response.Write(path); 38 } 39 catch (Exception ex) 40 { 41 context.Response.Write("Error"); 42 //记录日志 43 new Common.LogHelper(typeof(UploadFileHandler)).Error(ex); 44 } 45 } 46 47 public bool IsReusable 48 { 49 get 50 { 51 return false; 52 } 53 } 54 } 55 public static class FileTypes 56 { 57 private static List
allowedFileTypes = new List
(); 58 //获取允许json配置文件 59 private static string jsonFilePath = Common.PathHelper.MapPath("~/AllowedFileTypes.json"); 60 61 ///
62 /// 允许的文件类型 63 /// 64 public static List
AllowedFileTypes 65 { 66 get 67 { 68 return allowedFileTypes; 69 } 70 71 set 72 { 73 allowedFileTypes = value; 74 } 75 } 76 77 ///
78 /// 静态构造方法 79 /// 80 static FileTypes() 81 { 82

转载于:https://www.cnblogs.com/weihanli/p/fileUploadWithoutRefresh.html

你可能感兴趣的文章
Yii2的一些问题
查看>>
LeetCode OJ - Populating Next Right Pointers in Each Node II
查看>>
C++ wifstream读取日文方法(中文适用)
查看>>
B-树
查看>>
php计算上个月是几月份
查看>>
浅谈 trie树 及其实现
查看>>
60款很酷的 jQuery 幻灯片演示和下载
查看>>
nyoj-20-吝啬的国度(深搜)
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
spark 2.4安装
查看>>
Embeded linux之移植boa
查看>>
C之变量初始化的重要性
查看>>
jQuery 学习笔记(jQuery: The Return Flight)
查看>>
Java中常用的测试工具JUnit
查看>>
PHP图形图像的典型应用 --常用图像的应用(验证码)
查看>>
Robot Framework-Ride界面介绍及库的添加
查看>>
IntelliJ IDEA 连接数据库 详细过程
查看>>
redis完全攻略
查看>>
D3---01基础的柱状图制作(转)
查看>>
Time-Varying Mesh Compression
查看>>