简体   繁体   English

在Kendo Grid中追加文件

[英]Appending file in Kendo Grid

I'm trying to upload a file using Kendo Grid , using Kendo Upload is not an option because model and the file are related and upload cannot happen separately. 我正在尝试使用Kendo Grid上传文件,因此不能选择使用Kendo Upload因为模型和文件是相关的,并且上传不能单独进行。

I've considered two options, having a HttpPostedFileBase field in the model and setting it in the save event. 我考虑了两个选项,在模型中具有HttpPostedFileBase字段,并在save事件中进行设置。 And receiving that file as an argument in the action method and using Data method of update and create configurations. 并在action method使用该文件作为参数并使用Data方法updatecreate配置。 Both of these ways will end up with client-side kendo exceptions like JavaScript runtime error: Argument not optional . 这两种方式最终都会出现客户端kendo异常,例如JavaScript runtime error: Argument not optional

I'll be grateful if you could guide me with this. 如果您能指导我,我将不胜感激。

Action method signature: Action method签名:

public async Task<ActionResult> Update([DataSourceRequest] DataSourceRequest request, myViewModel model, HttpPostedFileBase file)

Client side functions: 客户端功能:

var descRes = new function () {
  this.onSave = function (e) {
        var u2 = $('#upload2');
        var data = new FormData();
        var files = $('#upload2').get(0).files;
        if (files.length > 0)
            data.append('file', files[0]);

        e.model.set('ImageFile', files[0]); // I have tried to set it to `data` too
    };

    this.getFile = function (e) {
        var data = new FormData();
        var files = $('#upload2').get(0).files;
        if (files.length > 0)
            data.append('file', files[0]);

        return { file: data };
        // return data; // I have also tried this, or returning `files[0]` directly
    };
};

Grid's configuration: 网格的配置:

@(Html.Kendo().Grid<viewModel>()
    .Name("grid")
    // removed for brevity 
    .Editable(e => e.Mode(GridEditMode.InLine))
    .Events(e => e
        .Save("descRes.onSave")
    )
    .DataSource(ds => ds
        .Ajax()
        .Model(m =>
        {
            m.Id(x => x.Id);
        })
        .Read(r => r.Action("Read", "AssetRes").Data("descRes.readData"))
        .Create(c => c.Action("Create", "AssetRes").Data("descRes.getFile"))
        .Update(u => u.Action("Update", "AssetRes").Data("descRes.getFile"))
        .Destroy("Delete", "AssetRes")
    )
)

I'm also trying to use Kendo Upload separately and use the file's name in the grid's model as in this example , the problem is that the upload object doesn't have any functions, files and getFiles are undefined. 我也尝试单独使用Kendo Upload并在此示例中使用网格模型中的文件名,问题是上载对象没有任何功能, filesgetFiles未定义。

You can write a custom grid editor for the upload. 您可以编写用于上载的自定义网格编辑器。 The upload should work asyncly, so you could save your row only when the file is uploaded. 上载应异步进行,因此只有在上载文件时才可以保存行。 In your model you then write the TempFileID returned by the upload. 然后,在模型中,编写上载返回的TempFileID。 This way I created grids with uploads and further data. 这样,我使用上传和更多数据创建了网格。 It's a little bit tricky, but possible. 这有点棘手,但可能。

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

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