简体   繁体   English

显示已存在的文件jQuery File Uploader时出现问题

[英]trouble showing already existing files jQuery File Uploader

I've been beating my head over this for quite a while now, so I think it's time I reach out for help. 我现在已经在这方面打了很长时间了,所以我觉得是时候伸手去寻求帮助了。 I have some already existing code that uses the jQuery File Uploader plugin, allowing me to upload files to my webserver. 我有一些已经存在的代码使用jQuery File Uploader插件,允许我将文件上传到我的web服务器。 The trouble I am having is listing files that already exist on the web server. 我遇到的麻烦是列出Web服务器上已存在的文件。

Here is my initialization code that runs at the client : 这是我在客户端运行的初始化代码

        $('#fileupload').fileupload({
            disableImageResize: false,
            url: '/api/upload',
            done: function (e, data) { // data is checked here }
        });

        // Load existing files:
        $('#fileupload').addClass('fileupload-processing');
        $.ajax({
            url: $('#fileupload').fileupload('option', 'url'),
            dataType: 'json',
            context: $('#fileupload')[0],
            data: { action: "FileList", blob: "uts", path: "Unit 14/Binaries/" }
        }).always(function (e, data) {
            $(this).removeClass('fileupload-processing');
        }).done(function (result) {
            $(this).fileupload('option', 'done')
                .call(this, $.Event('done'), { result: result });
        });

Now, I am trying to return a list of pre-existing files on the server side that matches the JSON response akin to the documentation . 现在,我试图返回服务器端的预先存在的文件列表,该列表与文档JSON响应相匹配。 My ASP.NET code on the server side is as follows (with two bogus files called "Something" and "SomethingElse" using my FilesStatus class ). 在服务器端的 ASP.NET 代码如下(使用我的FilesStatus类,有两个名为“Something”和“SomethingElse”的虚假文件)。

    // Get a list of files from 
    private void FileList(HttpContext hc)
    {
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
        List<FilesStatus> fs_list = new List<FilesStatus>();

        fs_list.Add(new FilesStatus("Something", 124));
        fs_list.Add(new FilesStatus("SomethingElse", 124));

        HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("Cache-Control", "private, no-cache");
        hc.Response.AddHeader("Content-Disposition", "inline; filename=\"files.json\"");
        var result = new { files = fs_list.ToArray() };
        hc.Response.Write(serializer.Serialize(result));
        hc.Response.ContentType = "application/json";
        HttpContext.Current.Response.StatusCode = 200;
    }

In the "done" function of the AJAX code, I see what I believe is the proper response on the client side. 在AJAX代码的“完成”功能中,我看到我认为客户端的正确响应 Here, you can see the format in my Javascript debugger (ie, a top level "files" that is an array): 在这里,您可以在我的Javascript调试器中看到格式(即,作为数组的顶级“文件”):

在此输入图像描述

These files do not get populated in to the file list, though. 但是,这些文件不会填充到文件列表中。 The code that I marked "//data is checked here" in the main done() function shows that the array can be accessed as "data.result.files" NOT "data.files." 我在main done()函数中标记为“// data here here”的代码显示该数组可以作为“data.result.files”而非“data.files”访问。 I can change ".call(this, $.Event('done'), { result: result });" 我可以改变“.call(this,$ .Event('done'),{result:result});” to ".call(this, $.Event('done'), { files: result.files });" “.call(this,$ .Event('done'),{files:result.files});” so that "data.files" is the location of the file array, but this does not solve the problem. 所以“data.files”是文件数组的位置,但这并不能解决问题。 I can't seem to get any pre-existing files to load in to the list. 我似乎无法将任何预先存在的文件加载到列表中。

Does anyone see what I am doing wrong? 有谁看到我做错了什么? Happy holidays. 节日快乐。

What happens when you change the line: 更改线路时会发生什么:

hc.Response.Write(serializer.Serialize(result));

into

hc.Response.Write(serializer.Serialize(fs_list.ToArray()));

It looks like the serializer is taking the variable name into account when you are serializing your file descriptions. 在序列化文件描述时,看起来序列化程序会将变量名称考虑在内。 The 'result' JSON object should disappear from the response. 'result'JSON对象应该从响应中消失。

I was overwriting the done() method where I have: 我覆盖了我所拥有的done()方法:

done: function (e, data) { // data is checked here }

I did this for debugging, but apparently it blocks the pre-existing file list from being loaded and calling the download template. 我这样做是为了调试,但显然它阻止了预先存在的文件列表被加载并调用下载模板。

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

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