【问题标题】:trouble showing already existing files jQuery File Uploader无法显示已经存在的文件 jQuery File Uploader
【发布时间】:2014-01-12 02:17:39
【问题描述】:

我已经为此苦恼了很久,所以我想是时候寻求帮助了。我有一些使用 jQuery File Uploader 插件的现有代码,允许我将文件上传到我的网络服务器。我遇到的问题是列出 Web 服务器上已经存在的文件。

这是我的初始化在客户端运行的代码

        $('#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 });
        });

现在,我正在尝试返回与JSON response akin to the documentation 匹配的服务器端预先存在的文件列表。我的 ASP.NET 服务器端代码如下(使用我的FilesStatus class 有两个名为“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;
    }

在 AJAX 代码的“完成”功能中,我看到 我认为是客户端的正确响应。在这里,您可以在我的 Javascript 调试器中看到格式(即,作为数组的顶级“文件”):

但是,这些文件不会填充到文件列表中。我在主 done() 函数中标记为“//data is checked here”的代码表明该数组可以作为“data.result.files”而不是“data.files”访问。我可以更改“.call(this, $.Event('done'), { result: result });”到“.call(this, $.Event('done'), { files: result.files });”这样“data.files”就是文件数组的位置,但这并不能解决问题。我似乎无法将任何预先存在的文件加载到列表中。

有人看到我做错了吗?节日快乐。

【问题讨论】:

    标签: javascript jquery asp.net json jquery-file-upload


    【解决方案1】:

    换行时会发生什么:

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

    进入

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

    在您序列化文件描述时,序列化程序似乎正在考虑变量名称。 'result' JSON 对象应该从响应中消失。

    【讨论】:

    • 我认为“result”仅仅是因为“done”函数的参数被命名为“result”。不幸的是,这没有帮助:\感谢您的回复!
    • 我解决了我的错误,但我不能给自己赏金......所以我只想把它给你试图帮助我。享受吧。
    【解决方案2】:

    我正在覆盖我拥有的 done() 方法:

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

    我这样做是为了调试,但显然它会阻止加载预先存在的文件列表并调用下载模板。

    【讨论】:

      猜你喜欢
      • 2013-09-22
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      相关资源
      最近更新 更多