zoukankan      html  css  js  c++  java
  • asp.net异步上传

    界面如下:

    需要用到MyAjaxForm.js库

    http://code1.okbase.net/codefile/MyAjaxForm.js_2013012620457_83.htm

    <script src="~/Scripts/MyAjaxForm.js"></script>

     @using (Ajax.BeginForm("AjaxAddUserPrintReport", "Admin", new AjaxOptions() { HttpMethod = "post", LoadingElementId = "loading", OnSuccess = "afterOk",OnFailure="afterErr" }, new { id = "addform", @class = "form-horizontal", enctype = "multipart/form-data" }))
        {

    <div class="form-group"> <label for="uploadfile" class="col-md-2 control-label"> 上传文件:</label> <div class="col-md-4"> <textarea class="form-control hidden" id="Uploadfileurl" name="Uploadfileurl" rows="3"></textarea> <input type="file" id="fileup" name="fileup" /> <button type="button" id="btnupload" class="btn btn-warning btn-sm">上传</button> </div> <div id="showfile" class="col-md-4"> </div> </div>
    }


      //点击上传文件
            $("#btnupload").click(function () {
                if ($(":file").val() == "")
                {
                    alert("请选择上传文件后再点击上传按钮");
                    return;
                }
                $("#addform").ajaxSubmit({
                    error: function (error) { alert(error); },
                    url: '/Admin/AjaxUploadFile',
                    type: "post",
                    success: function (data) {
                        var arr = data.split(":");
                        if (arr[0] == "ok") {
                            $("#Uploadfileurl").val($("#Uploadfileurl").val().trim()==""?arr[1]: $("#Uploadfileurl").val()+ "," + arr[1]);
                            addfile();
                        }
                        else {
                            alert(arr[1]);
                        }
                    }                
                });
            });

     MVC中上传代码如下:

     /// <summary>
            /// 上传文件,最大上传10M
            /// </summary>
            /// <returns></returns>

    public ActionResult AjaxUploadFile() { try { HttpPostedFileBase uploadfile = Request.Files["fileup"]; if (uploadfile == null) { return Content("no:非法上传"); } if (uploadfile.FileName == "") { return Content("no:请选择文件"); } if (uploadfile.ContentLength > 10 * 1024 * 1024) { return Content("no:上传文件超过10M,实际上传大小为" + uploadfile.ContentLength); } string filename = Path.GetFileName(uploadfile.FileName); string fileExt = Path.GetExtension(filename); StringBuilder sbtime = new StringBuilder(); sbtime.Append(DateTime.Now.Year).Append(DateTime.Now.Month).Append(DateTime.Now.Day).Append(DateTime.Now.Hour).Append(DateTime.Now.Minute).Append(DateTime.Now.Second); string dir = "/UploadFile/" + getUserBySession().Name + "/" + filename.Substring(0, filename.LastIndexOf(".")) + "_" + sbtime.ToString() + fileExt; string realfilepath = Request.MapPath(dir); string readDir = Path.GetDirectoryName(realfilepath); if (!Directory.Exists(readDir)) Directory.CreateDirectory(readDir); uploadfile.SaveAs(realfilepath); return Content("ok:" + dir); } catch (Exception ex) { return Content("no:" + ex.Message); }
  • 相关阅读:
    antd pro v5安装并运行完整demo的方法
    react项目运行在微信公众号
    nginx服务器上部署react项目
    毕业后,初入社会的困境和挣扎
    win10系统无法执行exe文件,解决方法
    前端学习11.14
    前端学习11.13
    Struts学习-Hibernate2
    Struts学习-Hibernate
    Struts2学习-自动
  • 原文地址:https://www.cnblogs.com/lunawzh/p/4498005.html
Copyright © 2011-2022 走看看