如何实现旋转器或进度栏?

发布于 2025-02-13 18:57:49 字数 1705 浏览 0 评论 0原文

我有一个表单数据,该数据将文件传递给IFORMFILE以读取Excel文件中的数据。我想在方法读取时添加一些旋转器并将数据上传到数据库。我正在从按钮提交中传递文件...看到一些可以使用Ajax完成的帖子,因为我不确定如何在代码中实现,而是使用Ajax尝试了几种方法,但是对FormData有问题。

<form enctype="multipart/form-data" method="post" asp-controller="UploadController" asp-action="Upload">
        <div class="mt-5">
            <div class="row">
                <div class="col-6 mb-3">
                    <h3>2. Upload a File</h3>
                    <div class="custom-file">
                        <input type="file" class="custom-file-input" name="file" />
                        <label class="custom-file-label" for="customFile">Choose file</label>
                        <i>Only Excel file .xls, .xlsx</i>
                    </div>
                </div>
            </div>
        </div>
        <div class="mt-5">
            <div class="row">
                <div class="col-6">
                    <div class="float-left">
                        <button type="submit" id="submit" name="Submit" class="btn btn-info">Upload</button>
                        <button asp-controller="Exposure" asp-action="Index" type="submit" class="btn btn-light" data-dismiss="modal">Back to List</button>
                    </div>
                </div>
            </div>
        </div>
    </form>

<---Method---->
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Upload(IFormFile file)
{
   if(file != null)
   {
   
   }
}

I have a form data that passes file to IFormFile to read the data in an excel file. I want to add some spinner while my method read's and uploads the data to my database. I'm passing the file from button submit... Saw some post that it can be done using Ajax, for reason I'm not sure how to implement in my code, tried some several methods using Ajax however having issue with the formdata.

<form enctype="multipart/form-data" method="post" asp-controller="UploadController" asp-action="Upload">
        <div class="mt-5">
            <div class="row">
                <div class="col-6 mb-3">
                    <h3>2. Upload a File</h3>
                    <div class="custom-file">
                        <input type="file" class="custom-file-input" name="file" />
                        <label class="custom-file-label" for="customFile">Choose file</label>
                        <i>Only Excel file .xls, .xlsx</i>
                    </div>
                </div>
            </div>
        </div>
        <div class="mt-5">
            <div class="row">
                <div class="col-6">
                    <div class="float-left">
                        <button type="submit" id="submit" name="Submit" class="btn btn-info">Upload</button>
                        <button asp-controller="Exposure" asp-action="Index" type="submit" class="btn btn-light" data-dismiss="modal">Back to List</button>
                    </div>
                </div>
            </div>
        </div>
    </form>

<---Method---->
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Upload(IFormFile file)
{
   if(file != null)
   {
   
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城仅有旧梦在 2025-02-20 18:57:49

我更改您的代码,并通过JavaScript添加进度栏,您可以参考以下演示:

<form enctype="multipart/form-data" method="post" asp-controller="UploadController" asp-action="Upload">
    <div class="mt-5">
        <div class="row">
            <div class="col-6 mb-3">
                <h3>2. Upload a File</h3>
                <div class="custom-file">
                    <input type="file" class="custom-file-input" name="file" id="File" />
                    <label class="custom-file-label" for="customFile" >Choose file</label>
                    <i>Only Excel file .xls, .xlsx</i>
                </div>
            </div>
        </div>
    </div>
    <div class="mt-5">
        <div class="row">
            <div class="col-6">
                <div class="float-left">
                    <button id="submit" name="Submit" class="btn btn-info" onclick="UploadImage()">Upload</button>
                    <button asp-controller="Exposure" asp-action="Index" type="submit" class="btn btn-light" data-dismiss="modal">Back to List</button>
                </div>
            </div>
        </div>
    </div>
</form>

<div class="progress">
    <div class="progress-bar progress-bar-success progress-bar-striped 
    active" role="progressbar"
         aria-valuemin="0" aria-valuemax="100" style="width:0%">0%</div>
</div>

<script>
   function UploadImage(){
       var data = new FormData;
       var fileInput = document.getElementById("File");
       var files = fileInput.files;
      for(var i = 0; i < files.length; i++) {
        data.append("files",files[i])
      }
      
      $.ajax({
        xhr: function() {
        var xhr = new window.XMLHttpRequest();
      
        xhr.upload.addEventListener("progress", function(evt) {
          if (evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            percentComplete = parseInt(percentComplete * 100);
      
            $('.progress-bar').width(percentComplete+'%');
            $('.progress-bar').html(percentComplete+'%');
      
           }
        }, false);
      
         return xhr;
        },
        url: '/UploadController/Upload',
        type: 'POST',
        data: data,
        contentType: false,
        processData: false,
        success: function(result) {
          console.log(result);
        }
      });
   }
</script>

控制器

   [HttpPost]
    public async Task<IActionResult> Upload(IList<IFormFile> files)
    {
        //......
    }

然后,当您上传文件时,将有一个进度栏。

< < a href =“ https://i.sstatic.net/eecy2.png” rel =“ nofollow noreferrer”> “在此处输入图像说明”

I change your code and add a progress bar by javascript, you can refer to the following demo:

<form enctype="multipart/form-data" method="post" asp-controller="UploadController" asp-action="Upload">
    <div class="mt-5">
        <div class="row">
            <div class="col-6 mb-3">
                <h3>2. Upload a File</h3>
                <div class="custom-file">
                    <input type="file" class="custom-file-input" name="file" id="File" />
                    <label class="custom-file-label" for="customFile" >Choose file</label>
                    <i>Only Excel file .xls, .xlsx</i>
                </div>
            </div>
        </div>
    </div>
    <div class="mt-5">
        <div class="row">
            <div class="col-6">
                <div class="float-left">
                    <button id="submit" name="Submit" class="btn btn-info" onclick="UploadImage()">Upload</button>
                    <button asp-controller="Exposure" asp-action="Index" type="submit" class="btn btn-light" data-dismiss="modal">Back to List</button>
                </div>
            </div>
        </div>
    </div>
</form>

<div class="progress">
    <div class="progress-bar progress-bar-success progress-bar-striped 
    active" role="progressbar"
         aria-valuemin="0" aria-valuemax="100" style="width:0%">0%</div>
</div>

<script>
   function UploadImage(){
       var data = new FormData;
       var fileInput = document.getElementById("File");
       var files = fileInput.files;
      for(var i = 0; i < files.length; i++) {
        data.append("files",files[i])
      }
      
      $.ajax({
        xhr: function() {
        var xhr = new window.XMLHttpRequest();
      
        xhr.upload.addEventListener("progress", function(evt) {
          if (evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            percentComplete = parseInt(percentComplete * 100);
      
            $('.progress-bar').width(percentComplete+'%');
            $('.progress-bar').html(percentComplete+'%');
      
           }
        }, false);
      
         return xhr;
        },
        url: '/UploadController/Upload',
        type: 'POST',
        data: data,
        contentType: false,
        processData: false,
        success: function(result) {
          console.log(result);
        }
      });
   }
</script>

Controller

   [HttpPost]
    public async Task<IActionResult> Upload(IList<IFormFile> files)
    {
        //......
    }

Then when you upload a file, There will be a progress bar.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文