【问题标题】:How to show the progress while uploading a file in Asp.net MVC web application, c#?如何在 Asp.net MVC Web 应用程序 C# 中上传文件时显示进度?
【发布时间】:2019-08-09 17:31:00
【问题描述】:

在我的 asp.net 网络应用程序中,我有一个上传文件的部分,它工作正常,但问题是用户看不到进度,当他们想要上传更大的文件时,这尤其非常必要。

所以我的问题是:上传文件时如何显示进度(简单的进度条或百分比)。 我尝试了这个论坛中的一些解决方案,但没有一个对我有用。

这里是控制器:

 public ActionResult BriefDetails(int? ID)
    {
       var dtl = _context.Original.SingleOrDefault(b => b.Id == Id);
        var vm = new BriefUploadVM()

        {
            Id = dtl.Id,
            brief_rp = dtl.brief_rp,
        };
        return View(vm);
    }

这是视图模型:

 public class BriefUploadVM
{
    public int Id { get; set; }
    public string BriefReport { get; set; }

    [Required(ErrorMessage = "Error: Attach your file")]
    [NotMapped]
    public HttpPostedFileBase brief_rp { get; set; }
}

这是视图

@using (Html.BeginForm("Store", "Reports", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

<div class="form-group">

    <input type="file" name="brief_rp" />
    @Html.ValidationMessageFor(a=> a.brief_rp)
</div>

@Html.AntiForgeryToken();
@Html.HiddenFor(a => a.Id);
<button class="btn btn-primary">Submit</button>

}

这是控制器内部的存储方法

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Store(BriefUploadVM rp)
    {



            string Second_filename = Path.GetFileNameWithoutExtension(rp.brief_rp.FileName);
            string Second_extension = Path.GetExtension(rp.brief_rp.FileName);
            Second_filename = Second_filename + DateTime.Now.ToString("yymmssfff") + Second_extension;
            rp.BriefReport = "Files/" + Second_filename;
            Second_filename = Path.Combine(Server.MapPath("~/Files/"), Second_filename);
            rp.brief_rp.SaveAs(Second_filename);


            var item = _context.original.Single(a => a.Id == rp.Id);
            item.Brief_Report = rp.BriefReport;
            _context.SaveChanges();
            return RedirectToAction("Success", "Project");



    }

【问题讨论】:

  • “我尝试了这个论坛中的一些解决方案,但没有一个对我有用。”为什么不?您将这个问题与其他问题区分开来的具体要求是什么?
  • "...但是没有一个对我有用..." - 这些信息并没有真正的帮助。它没有说明究竟是什么不起作用,即您是否在任何地方收到任何错误消息,您是否尝试调试问题以确保它确实完成了预期的事情。
  • 那些不适用于网络应用程序@JohnB
  • 答案不容易想出。你可以试试这样的codereview.stackexchange.com/questions/85790/…codeguru.com/csharp/.net/…

标签: c# asp.net-mvc


【解决方案1】:

您可以使用 jQuery 表单插件:http://malsup.com/jquery/form/#file-upload 来满足您的要求。

这是一个示例。你可以参考。希望能帮到你,我的朋友:))

---控制器---

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AsynFileUpload(IEnumerable<HttpPostedFileBase> files)
        {
            int count = 0;
            if (files != null)
            {
                foreach (var file in files)
                {
                    if (file != null && file.ContentLength > 0)
                    {                        
                        var path = Path.Combine(Server.MapPath("~/UploadedFiles"), file.FileName);
                        file.SaveAs(path);
                        count++;
                    }
                }
            }
            return new JsonResult
            {
                Data = "Successfully " + count + " file(s) uploaded"
            };
        }

---查看----

<style>
    .progress {
        position: relative;
        width: 400px;
        border: 1px solid #ddd;
        padding: 1px;
        border-radius: 3px;
    }

    .bar {
        background-color: #B4F5B4;
        width: 0%;
        height: 20px;
        border-radius: 3px;
    }

    .percent {
        position: absolute;
        display: inline-block;
        top: 3px;
        left: 48%;
    }
</style>

<h2> AsynFileUpload </h2>

@using (Ajax.BeginForm("AsynFileUpload", "Home", new AjaxOptions() { HttpMethod = "post" }, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <input type="file" name="files" multiple />
    <br />
    <input type="submit" value="upload file" />    
}
<br />
<div class="progress">
    <div class="bar"></div>
    <br />
    <div class="percent">0%</div>
</div>
<div id="status"></div>

@section scripts {
    <script src="https://malsup.github.io/min/jquery.form.min.js"></script>
    <script>
        (function () {

            var bar = $('.bar');
            var percent = $('.percent');
            var status = $('#status');

            $('form').ajaxForm({
                beforeSend: function () {
                    status.empty();
                    var percentVal = '0%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                uploadProgress: function (event, position, total, percentComplete) {
                    var percentVal = percentComplete + '%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                success: function () {
                    var percentVal = '100%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                complete: function (xhr) {
                    status.html(xhr.responseText);
                }
            });

        })();
    </script>
}

此外,默认上传文件大小为 4MB。要增加它,请在您的 web.config 中使用以下部分 -

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="1048576" />
    </system.web>
</configuration>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多