基於AdminLTE的jquery頭像更新

來源:互聯網
上載者:User

標籤: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頭像更新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.