【问题标题】:How to get multiselected files from input to c# object如何从输入中获取多选文件到c#对象
【发布时间】:2018-01-15 20:11:26
【问题描述】:

我想将输入文件中选择的所有文件都获取到 c# 对象,但它只从多选文件中选择一个文件显示在文本框中。

输入文件选择多个文件:

<input type="file" name="File2" id="File2" accept="image/*" multiple/> 

输入文本以显示所有选定的文件:

@Html.EditorFor(model => model.DocumentName, new { htmlAttributes = new { @id = "documentName", @class= "form-control" } })

型号:

 [Display(Name = "DocumentName", ResourceType = typeof(Resources.Resources))]
        公共覆盖字符串 DocumentName
        {
            得到{返回base.DocumentName; }
            设置 { base.DocumentName = 值; }
        }

我的代码需要进行哪些更改才能解决?

【问题讨论】:

  • 您可以尝试 jquery 来显示选定的文件。如果能够使用 jquery 意味着我将分享如何做到这一点。
  • 请解释一下如何?
  • 你能修改我的代码来解决我的问题吗?

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

请将此添加到您的脚本中。它在选择上传文件时起作用,即 OnChange 功能。请尝试并告诉我。

 $("document").ready(function(){
$("#File2").change(function() {
    var files = $(this)[0].files;
for (var i = 0; i < files.length; i++) {
    $("#documentName").val(files[i].name);
}    });});

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2017-09-08
    • 2015-11-07
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    相关资源
    最近更新 更多