简体   繁体   English

解决:blueimp / jQuery-File-Upload无法与ASP.NET Core Razor Pages一起使用

[英]Solved: blueimp / jQuery-File-Upload doesn't work with ASP.NET Core Razor Pages

I develop a website with asp.net core 2.1 razor pages, where the user can drop several media files to the site or selected the files with click on the button to upload the files to the server. 我用asp.net核心2.1剃须刀页面开发了一个网站,用户可以在其中将多个媒体文件拖放到该站点,或者通过单击按钮将文件上传到服务器来选择文件。 Then the files are shown on the page with name and other attributes. 然后,文件将显示在页面上,并带有名称和其他属性。 Each file have an delete button to delete the upload. 每个文件都有一个删除按钮以删除上载。

After all files are selected or dropped, the user press the submit button and all files will be submitted together with several formfields to the server. 在选择或删除所有文件之后,用户按下“提交”按钮,所有文件将连同几个表单字段一起提交到服务器。

The problem is now, that no files were send to the server. 现在的问题是,没有文件发送到服务器。 The display on the web site with the thumbs are correct, but after submit HttpContext.Request.Form.Files is empty. 网站上显示的大拇指是正确的,但是提交HttpContext.Request.Form.Files后为空。

After I have added the option '''replaceFileInput: false''', the files are added by the button are submitted correctly. 添加选项'''replaceFileInput:false'''之后,通过按钮添加的文件将正确提交。

If the user drag & drop files, the files were not added to the input-object. 如果用户拖放文件,则文件未添加到输入对象。 After the submit, this files were not submitted to the server. 提交后,此文件未提交到服务器。

A demo of the website are hosted on github: https://github.com/IsibisiCoder/Asp.Net-Core-with-blueimp-jQuery-File-Upload 该网站的演示位于github上: https : //github.com/IsibisiCoder/Asp.Net-Core-with-blueimp-jQuery-File-Upload

index.cshtml: index.cshtml:

<form id="fileupload" method="POST" data-url="Index" enctype="multipart/form-data">
<span class="btn btn-primary fileinput-button upload-button">
    <span>add files...</span>
    <input type="file" name="files" multiple>
</span>
<span class="fileupload-process"></span>

<!-- The table listing the files available for upload/download -->
<div id="upload-table-hide">
    <div id="upload-table table-wrapper">
        <div id="table-scroll">
            <table role="presentation" class="table table-striped"><tbody id="uploadedfiles" class="files"></tbody></table>
        </div>
    </div>
</div>
<div class="form-row">
    <div class="form-group col-sm-6">
        <input asp-for="Value1" class="form-control" placeholder="Parameter Formdata 1" />
        <span asp-validation-for="Value1" class="text-danger"></span>
    </div>
    <div class="form-group col-sm-6">
        <input asp-for="Value2" class="form-control" placeholder="Parameter Formdata 2" />
        <span asp-validation-for="Value2" class="text-danger"></span>
    </div>
</div>

<div class="form-row">
    <div class="col-sm-auto">
        <button id="upload-button" type="submit" class="btn btn-primary upload-button start">
            <span>upload</span>
        </button>
    </div>
</div>
</form>

@section Scripts{

<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade show">
    <td>
        <span class="preview"></span>
    </td>
    <td>
        <p class="name">{%=file.name%}</p>
        <strong class="error text-danger"></strong>
    </td>
    <td>
        <p class="size">Processing...</p>
    </td>
    <td>
        {% if (!i) { %}
        <button class="btn btn-warning upload-button cancel">
            <span>Löschen</span>
        </button>
        {% } %}
    </td>
</tr>
{% } %}
</script>

<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade show">
    <td>
        <span class="preview"></span>
    </td>
    <td>
        <p class="name">
            <span>{%=file.name%}</span>
        </p>
        {% if (file.error) { %}
        <div><span class="label label-danger">Error</span> {%=file.error%}</div>
        {% } %}
    </td>
    <td>
        <span class="size">{%=o.formatFileSize(file.size)%}</span>
    </td>
    <td>
        <p>&#9989;</p>
    </td>
</tr>
{% } %}
</script>

<script type="text/javascript">
    $(function (e, data) {
        'use strict';
        // Initialize the jQuery File Upload widget:
        $('#fileupload').fileupload({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            previewMaxWidth: 100000,
            autoUpload: false,
            replaceFileInput: false,
            dataType: 'json',
            singleFileUploads: false,
            multipart: true,
            limitMultiFileUploadSize: 10000,
            paramName: 'files'
        });
    });
</script>
}

index.cshtml.cs index.cshtml.cs

public class IndexModel : PageModel
{
    [BindProperty]
    public string Value1 { get; set; }

    [BindProperty]
    public string Value2 { get; set; }

    public void OnGet()
    {

    }

    [HttpPost]
    public IActionResult OnPost(List<IFormFile> files)
    {
        // with parameter or httpcontext or both
        var files2 = HttpContext.Request.Form.Files;
        return Page();
    }
}

I have found a project with asp.net mvc on https://github.com/ronnieoverby/blue-imp-upload-aspnet-mvc , that works fine, the request.files are filled. 我在https://github.com/ronnieoverby/blue-imp-upload-aspnet-mvc上找到了一个使用asp.net mvc的项目,该项目工作正常,request.files已填充。 But with asp.net core the source doesn't work. 但是,使用asp.net核心无法正常工作。

(Update) I have change the description and added a demo to github. (更新)我更改了说明,并向github添加了一个演示。

(Update) I have solved the problem. (更新)我已经解决了问题。 See my separate post from today. 请参阅我今天的另外一篇文章。

您缺少表单的data-url属性,请将以下属性添加到表单。

data-url="/"

Check the difference from your code and steps below: 检查与您的代码和以下步骤的区别:

  1. Create Asp.Net Core Razor Page 2.1 template 创建Asp.Net Core Razor Page 2.1模板
  2. Add Code below to Index.cshtml 将下面的代码添加到Index.cshtml

     <form id="fileupload" method="POST" enctype="multipart/form-data"> <span class="btn btn-primary fileinput-button upload-button"> <span>add files...</span> <input type="file" name="files[]" multiple> </span> <span class="fileupload-process"></span> <!-- The table listing the files available for upload/download --> <div id="upload-table-hide"> <div id="upload-table table-wrapper"> <div id="table-scroll"> <table role="presentation" class="table table-striped"><tbody id="uploadedfiles" class="files"></tbody></table> </div> </div> </div> <div class="form-row"> <div class="col-sm-auto"> <button id="upload-button" type="submit" class="btn btn-primary upload-button start"> <span>upload</span> </button> </div> </div> </form> @section Scripts{ <script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.32.0/js/jquery.fileupload.js"></script> <script type="text/javascript"> $(function (e, data) { 'use strict'; // Initialize the jQuery File Upload widget: $('#fileupload').fileupload({ // Uncomment the following to send cross-domain cookies: //xhrFields: {withCredentials: true}, previewMaxWidth: 100000000 }); }); </script> } 
  3. PageModel 页面模型

     public class IndexModel : PageModel { public void OnGet() { } public void OnPost() { var files = HttpContext.Request.Form.Files; } } 

I have solved the problem. 我已经解决了问题。

First: All files can not submitted together. 第一:所有文件不能一起提交。 For each file the post method will be called. 对于每个文件,将调用post方法。 There are two problems in my code: The submit button have to placed into a div-tag with the css-class "fileupload-buttonbar". 我的代码中有两个问题:提交按钮必须放在带有css类“ fileupload-buttonbar”的div标签中。

<div class="fileupload-buttonbar">
  <button type="submit" class="btn btn-primary start">
    <span>upload</span>
  </button>
</div>

The second problem are: I use the upload-template of the blueimp-framework and it is not allowed to submit single files. 第二个问题是:我使用blueimp-framework的上载模板,并且不允许提交单个文件。 There are only one submit button for all files. 所有文件只有一个提交按钮。 So I remove the start button in the upload-template. 因此,我删除了上传模板中的开始按钮。 This do not work, because the blueimp-framework used this start button internal. 这不起作用,因为blueimp-framework内部使用了此开始按钮。

The solution are to hide the element: 解决方案是隐藏元素:

<td>
    {% if (!i && !o.options.autoUpload) { %}
      <button class="btn btn-primary start" disabled style="display: none">
        <span>Start</span>
      </button>
    {% } %}
    {% if (!i) { %}
    <button class="btn btn-warning upload-button cancel">
      <span>Cancel</span>
    </button>
    {% } %}
</td>

My solution can be found at https://github.com/IsibisiCoder/Asp.Net-Core-with-blueimp-jQuery-File-Upload 我的解决方案可以在https://github.com/IsibisiCoder/Asp.Net-Core-with-blueimp-jQuery-File-Upload找到

I have found another solution with asp.net core 2.2 mvc at https://github.com/ronnieoverby/blue-imp-upload-aspnet-mvc 我在https://github.com/ronnieoverby/blue-imp-upload-aspnet-mvc找到了使用asp.net core 2.2 mvc的另一种解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Blueimp jQuery 文件上传与 ASP.NET Core 3.1 不兼容 - Blueimp jQuery File Upload not compatible with ASP.NET Core 3.1 ASP.NET Core 2.2 Razor Pages 为什么 OnPost() 有效,而 OnPostAsync() 无效? - ASP.NET Core 2.2 Razor Pages why does OnPost() work, but OnPostAsync() doesn't? 客户端验证不适用于Asp.Net Core Razor页面中的嵌套布局 - Client Side Validation Doesn't Work In Nested Layouts in Asp.Net Core Razor Pages 如何在 CKEditor 5 中使用 asp.net core razor Pages 上传图片 - How upload image in CKEditor 5 With asp.net core razor Pages Asp.net 核心 Razor 页面中的错误上传图像 - Error Upload image in Asp.net core Razor Pages ASP.NET Core (Razor Pages) - 添加文件上传到现有表单 - ASP.NET Core (Razor Pages) - Added file upload to existing form ASP.NET Core Razor Pages 中的 return Page() 在通过 RedirectToPage 调用后不呈现页面 - return Page() in ASP.NET Core Razor Pages doesn't render the page after calling by RedirectToPage 为什么 ASP.NET 核心 Razor 页面中的验证不接受空输入? - Why does validation in ASP.NET Core Razor Pages doesn't accept empty inputs? ASP.NET Razor 页面中的核心 localize.cs 文件 - ASP.NET Core localize .cs file in Razor Pages 我无法在 Razor 页面 (ASP.Net Core) 中加载我的 CSS 文件 - I can't get my CSS File to Load in my Razor Pages (ASP.Net Core)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM