最近在写实验室管理系统中的个人信息模块,上边要求实现更改头像功能。百度了一大堆,无实用的。(要么各种币)
本文介绍的只是实现了简单功能(毕竟现在初学阶段)
需要引用文件,顺序也不能错。
<script src="~/JS/bootstrap/js/jquery.min.js"></script>
<script src="~/JS/bootstrap/js/bootstrap.min.js"></script><script src="~/JS/jquery.ui.widget.js"></script><script src="~/JS/jquery.iframe-transport.js"></script><script src="~/JS/jquery.fileupload.js"></script>文件链接:http://pan.baidu.com/s/1skH0xnZ
jQuery
C#方法
public JsonResult img(HttpPostedFileBase files) { try { string localPath = "/images"; string path = Server.MapPath("~" + localPath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //TimeSpan ts = DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); //string fileName = (long)ts.TotalMilliseconds + Path.GetExtension(files.FileName); string fileName = files.FileName; files.SaveAs(path + "/" + fileName); return Json("{\"filePath\":\"" + localPath + "/" + fileName + "\",\"sourePath\":\"" + files.FileName + "\"}"); } catch (Exception ex) { return null; } }
可以实现简单头像更新。至此