jquery.form.js 實現非同步上傳

來源:互聯網
上載者:User

標籤:ret   rms   java   facebook   非同步   this   exists   before   get   

前台:

<form id="formSeacrh" action="/ResumeInfo/uploadFile" method="post" enctype="multipart/form-data">    <div class="form-horizontal">        <div class="form-group" style="height:100px;overflow-y:auto">            <div class="col-sm-8" id="filediv">                <div><input type="file" name="file" style="display: inline;" /><a href="javascript:void(0)" class="deletelink">刪除</a></div>                <div><input type="file" name="file" style="display: inline;" /><a href="javascript:void(0)" class="deletelink">刪除</a></div>                <div><input type="file" name="file" style="display: inline;" /><a href="javascript:void(0)" class="deletelink">刪除</a></div>            </div>        </div>        <div class="form-group">            <div class="col-sm-8">                <button type="button" class="btn btn-success" style="float:left" id="addbtn">添加附件</button>                <button type="submit" id="btnSubmit" style="float:right" class="btn btn-facebook">確定</button>            </div>        </div>    </div></form>

 

<script src="~/Scripts/jquery.form.js"></script><script>    $(function () {        $("#formSeacrh").submit(function () {            var options = {                //target: ‘#output‘,          //把伺服器返回的內容放入id為output的元素中                //beforeSubmit: showRequest,  //提交前的回呼函數                //url: url,                 //預設是form的action, 如果申明,則會覆蓋                //clearForm: true,          //成功提交後,清除所有表單元素的值                //resetForm: true,          //成功提交後,重設所有表單元素的值                //timeout: 3000,            //限制請求的時間,當請求大於3秒後,跳出請求                success: function (data) {                    if (data.message == "") {                        parent.addDetailToDiv(false, data.files);                        $(parent.document.getElementsByClassName(‘close‘)).click();                    } else {                        $.growl.warning({ message: data.message, title: "提示" });                    }                },                error: function (error) { alert(error); },                url: ‘/ResumeInfo/uploadFile‘, /*設定post提交到的頁面*/                type: "post", /*設定表單以post方法提交*/                dataType: "json" /*設定傳回值類型為文本*/            }            $(this).ajaxSubmit(options);            return false;        });        $(‘body‘).on(‘click‘, ‘#addbtn‘, addFile);        $(‘body‘).on(‘click‘, ‘.deletelink‘, deleteFile);    });    function addFile() {        $(‘#filediv‘).append(‘<div><input type="file" name="file" style="display: inline;" /><a href="javascript:void(0)" class="deletelink">刪除</a></div>‘);    }    function deleteFile() {        $(this).parent().remove();    }</script>

 

後台:

        /// <summary>        /// 上傳檔案        /// </summary>        /// <returns></returns>        public ActionResult uploadFile(string callback = "") {            ViewBag.callback = callback;            return View();        }        /// <summary>        /// 上傳檔案    提交        /// </summary>        /// <returns></returns>        [HttpPost]        public ActionResult uploadFile(List<HttpPostedFileBase> list) {            List<fileClass> fileNames = new List<fileClass>();            var msg = "";            if (Request.Files.Count == 0) {                msg = "請選擇檔案";            } else {                try {                    for (int i = 0; i < Request.Files.Count; i++) {                        if (Request.Files[i] != null) {                            string ext = Path.GetExtension(Request.Files[i].FileName);                            string filename = Guid.NewGuid().ToString() + ext;                            string path = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";                            string directory = Server.MapPath("~" + path);                            if (!Directory.Exists(directory)) {                                Directory.CreateDirectory(directory);                            }                            string fileFullName = Path.Combine(directory, filename);                            Request.Files[i].SaveAs(fileFullName);                            fileClass f = new fileClass();                            f.filename = Request.Files[i].FileName;                            f.path = path + filename;                            fileNames.Add(f);                        }                    }                } catch (Exception ex) {                    msg = ex.Message;                }            }            return Json(new { message = msg, files = fileNames });        }

 

jquery.form.js 實現非同步上傳

聯繫我們

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