简体   繁体   English

没有从文件上传接收任何数据

[英]Not receiving any data from file upload

I am trying to use the Krajee Bootstrap File Input to upload a file.我正在尝试使用Krajee Bootstrap 文件输入上传文件。 I am using ASP.NET Core 2. Here is my HTML:我正在使用 ASP.NET Core 2。这是我的 HTML:

<input id="fileFileUpload" type="file" />

Here is my javascript code:这是我的 javascript 代码:

    $("#fileFileUpload").fileinput({
        showPreview: true,
        uploadAsync: false,
        uploadUrl: '/fileupload'
    });

Here is my controller code:这是我的控制器代码:

[HttpPost]
public JsonResult Index(List<IFormFile> files)
{
    // Do stuff
}

I am putting a breakpoint on my controller method and the breakpoint is being hit.我在我的控制器方法上放置了一个断点,并且断点被击中。 However files is empty.但是files是空的。 How can I retrieve the file that was uploaded?如何检索已上传的文件?

The number one reason I can see for that code not work is that your input in not assigned a name.我可以看到该代码不起作用的第一个原因是您的输入未分配名称。

<input id="fileFileUpload" name="files" type="file" />

Other then that you can follow this MSDN article.除此之外,您可以关注这篇MSDN 文章。

You can access the files from another button and then post them using ajax as normal.您可以从另一个按钮访问文件,然后像往常一样使用 ajax 发布它们。 ButtonFileUpload8 is the ID for the new button, file-8 is the ID for the file upload control. ButtonFileUpload8 是新建按钮的ID,file-8 是文件上传控件的ID。 You have to disable the upload button您必须禁用上传按钮

This is not perfect code, I just put it together to show how its done.这不是完美的代码,我只是把它放在一起来展示它是如何完成的。

HTML: HTML:

    <form enctype="multipart/form-data">
        <div class="row mb-2">
            <div class="col-md-8">
                <div class="file-loading">
                    <input id="file-8" type="file" multiple>
                </div>
            </div>
        </div>
        <div class="btn-group btn-group-sm mr-1" role="group">
            <button id="ButtonFileUpload8" type="button" class="btn btn-sm">
                Blue
            </button>
        </div>

    </form>

Javascript (note this ignores the post URL set in the initiator so calls FileUploadKrajee instead and uses ajax): Javascript(注意这会忽略发起者中设置的 post URL,因此调用 FileUploadKrajee 并使用 ajax):

    $("#file-8").fileinput({
        uploadUrl:"@Url.Action("FileUploadKrajee", "App")",
        uploadAsync: true,
        minFileCount: 1,
        maxFileCount: 5,
        overwriteInitial: false,
        initialPreview: "",
        initialPreviewConfig: "",
        uploadExtraData: "",
        showUpload: false
    });

    //  Click of upload button

    $("#ButtonFileUpload8").click(function () {
        var control = document.getElementById("file-8");
        var files = control.files;
        var formData = new FormData();

        for (var i = 0; i != files.length; i++) {
            formData.append("files", files[i]);
        }

        $.ajax({
            url: 'UploadFiles3',
            type: "POST",
            contentType: false, // Do not set any content header
            processData: false, // Do not process data
            data: formData,
            async: false,
            success: function (result) {
                if (result != "") {
                    alert(result);
                }
                control.files.value("");
            },
            error: function (err) {
                alert(err.statusText);
            }
        });
    });

Then in the receiving controller you can use:然后在接收控制器中,您可以使用:

    public ActionResult UploadFiles3(List<IFormFile> files)
    {
        string nams = "";
        string funame = "";

        foreach (IFormFile source in files)
        {
            string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.ToString();

            filename = this.EnsureCorrectFilename(filename);

            nams = nams + source.FileName.ToString();

            funame = "D:\\Data\\PointsAlign\\Core\\" + filename;

            FileStream output = System.IO.File.Create(funame);

            source.CopyTo(output);
        }

        return Json("Hi, Alster. Your files uploaded successfully " + nams);

    }

This does work but I haven't stress tested it yet for any issues这确实有效,但我还没有针对任何问题对其进行压力测试

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM