jquery.form 和MVC4做無重新整理上傳DEMO

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   java   ar   for   

jquery.form 和MVC4做無重新整理上傳DEMO

HTML:

<script src="~/Scripts/jquery-1.10.2.min.js"></script><script type="text/javascript" src="/Scripts/jquery.form.js"></script><script type="text/javascript" src="/Scripts/common.js"></script>  <form accept-charset="UTF-8" id="node_form" method="post" action="">    <input size="30" class="tino_file" maxlength="128" name="pic" id="field-pic" imgstyle=" width=100" type="text" />    <input size="30" class="tino_file1" maxlength="128" name="pic1" id="field-pic1" imgstyle=" width=100" type="text" />        </form>

JS:

$(document).ready(function(){    $(‘input.tino_file‘).each(function(){        var form=$(this).parents("form");        form.attr(‘enctype‘,‘multipart/form-data‘);        var old_name=$(this).attr(‘name‘);        $(this).hide();        var new_name=old_name+‘_file‘;        var imgstyle=$(this).attr(‘imgstyle‘);        var imgstr=‘‘;        var old_file=$(this).val();        if(‘‘!=old_file)        {            imgstr=‘<a href="‘+old_file+‘" target="_blank"><img src="‘+old_file+‘" ‘+imgstyle+‘></a> <a href="###" onclick="del_pic(\‘‘+old_name+‘\‘)">刪除</a>‘;        }        var str=‘<input type="file" name="‘+new_name+‘" id="‘+new_name+‘" value=""><input type="hidden" id="‘+new_name+‘_del" value=""><div id="‘+new_name+‘_show">‘+imgstr+‘</div>‘;        $(this).after(str);        var ahah=$(this);            $(‘#‘+new_name).change(function(){            var tmp=$(this).val();            if(‘‘!=tmp)            {                //alert(ahah.val());                form.ajaxSubmit({                    url:"/Home/UploadImage",                    type: ‘POST‘,                    data:{‘field‘:new_name,‘only_img‘:‘yes‘,‘old_img‘:$(‘#‘+new_name+‘_del‘).val()},                    dataType: "json",                    //beforeSend:function(){loading_start();},                    success: function(result) {                        if (‘‘ != result.url) {                            var tmp = result;                                                    $(‘#‘ + new_name + ‘_show‘).html(‘<a href="‘ + tmp.url + ‘" target="_blank"><img src="‘ + tmp.realpath + ‘" ‘ + imgstyle + ‘></a> <a href="###" onclick="del_pic(\‘‘ + old_name + ‘\‘)">刪除</a>‘);                            ahah.val(tmp.url);                            $(‘#‘ + new_name + ‘_del‘).val(tmp.realpath);                                                    window.onbeforeunload = function () {                            }                        } else {                            alert("請選擇正確圖片上傳");                        }                                                                    }                });            }        });            });

Contoller:

        public JsonResult UploadImage()        {            var relativeurl = "";//相對路徑            var realurlpath = "";//絕對路徑            if (Request.Files.Count <= 0)                return Json(new                {                    url =relativeurl,                    realpath=realurlpath                });            for(var i = 0;i < Request.Files.Count;i++)            {                var extensionname = DateTime.Now.ToString("yyyyMMddmmss");                var file = HttpContext.Request.Files[i];                if (file == null || file.ContentLength <= 0) continue;                var originExtensionName = EnHtml(HttpUtility.UrlDecode(file.FileName,Encoding.GetEncoding("GB2312"))).Substring(EnHtml(HttpUtility.UrlDecode(file.FileName,Encoding.GetEncoding("GB2312"))).Length - 3);                if (originExtensionName.ToLower() != "jpg" && originExtensionName.ToLower() != "gif") continue;                var newFile = extensionname + "." + originExtensionName;                relativeurl = newFile;                realurlpath = "/upfile/" + newFile;                file.SaveAs(HttpContext.Server.MapPath("/upfile/" + newFile));            }            return Json(new {                url = relativeurl,                realpath = realurlpath            });        }        private static string EnHtml(string str) {            if(str == null)                return "";            str = str.Replace(" ","");            str = str.Trim();            return str;        }

 

jquery.form 和MVC4做無重新整理上傳DEMO

聯繫我們

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