AJAX 上傳圖片

來源:互聯網
上載者:User

引用jquery Form 外掛程式,地址:http://jquery.malsup.com/form/

<script type="text/javascript">    $(function () {        $("#btn_show").bind("click", function () {            $("#form_upload").show();            var options = {                success: function (responseText, statusText, xhr, $form) {                    var picPath = responseText.message;                    if (picPath == "") {                        alert(responseText.message);                    }                    else {                        $("#form_upload").hide();                        alert(unescape(responseText.message.replace(/\\/g, "%")));                        //                        $("#result").attr("src", picPath).show();                    }                },                error: function (XMLHttpRequest, textStatus, errorThrown) {                    console.log(textStatus);                    console.log(errorThrown);                }            };            $("#form_upload").ajaxForm(options);        });    });</script>

  

<input type="button" id="btn_show" value="上傳圖片" /><br />
<form id="form_upload" style="padding:20px;display:none" action="AddPic" method="post" enctype="multipart/form-data">
<input name="upImg" style="width:350px; height:25px;" size="38" type="file" />
<input type="submit" value="上傳"/>
</form>

 

後台C#代碼:

  [HttpPost]        public JsonResult AddPic(HttpPostedFileBase upImg)        {            string fileName = Path.GetFileName(upImg.FileName);            string filePhysicalPath = Server.MapPath("~/Upload/"+fileName);            string test = "\u56fe\u7247\u4ea0\u4f20\u6210\u529f";            string name = "", message = "";            byte[] bytes  = new byte[]{};            try            {                upImg.SaveAs(filePhysicalPath);                name = Path.GetFileName(fileName);                bytes = Encoding.Unicode.GetBytes("圖片添加成功");                for (int i = 0; i < bytes.Length; i+=2)                {                    message += "\\u" + bytes[i+1].ToString("x").PadLeft(2,'0')+bytes[i].ToString("x").PadRight(2,'0');                }            }            catch (Exception ex)            {                message = ex.Message;            }            return  Json(new {                name=name,                message = message            });        }

  

 

後台把中文轉換成Unicode代碼,傳輸後JS指令碼將Unicode再轉換成中文,最終通過彈框顯示到瀏覽器中。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.