標籤:user response get erro 實驗室 c# blog transport types
最近在寫實驗室管理系統中的個人資訊模組,上邊要求實現更改頭像功能。百度了一大堆,無實用的。(要麼各種幣)
本文介紹的只是實現了簡易功能(畢竟現在初學階段)
需要引用檔案,順序也不能錯。
<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
<div class="widget-user-image"> <img class="img-circle" id="appear" src="../../images/user.jpg" alt="User Avatar" > </div> <input type="file" id="fileupload" name="files" multiple> <button class=" btn btn-primary" id="Start">修改頭像</button>
jQuery
<script type="text/javascript"> $(‘#fileupload‘).fileupload({ url: "/Info/img", Type: "POST", dataType: ‘json‘, autoUpload: true, acceptFileTypes: "/(\.|\/)(gif|jpe?g|png|xlsx)$/i", add: function (e, data) { $("#Start").click(function () { data.submit(); }) alert("已選擇檔案,可以更新頭像!"); }, success: function (response, status) { var obj = JSON.parse(response); var imgPath = "../.." + obj["filePath"]; //$("#imglist").append(‘<li><img src="‘ + imgPath + ‘" /> </li>‘); $(‘#appear‘).attr("src",imgPath); }, done: function (e, data) { alert("update finish"); }, error: function (error) { alert("error"); }, });</script>
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; } }
可以實現簡單頭像更新。至此
基於AdminLTE的jquery頭像更新