jquery-plugins - jQuery 文件上传插件要求我下载文件,有什么问题吗?

标签 jquery-plugins asp.net-mvc-3

我正在使用 https://github.com/blueimp/jQuery-File-Upload并且我能够将文件上传并保存到指定的文件夹,然后返回 Json 对象。然后浏览器(我使用 IE8)弹出“文件下载”对话框,要求我下载一个名为“upload75bea5a4”的没有扩展名的文件。我就是想不通哪里出了问题?

最佳答案

我正在使用相同的插件,它对我来说没有任何问题。我将发布我正在使用的代码,以便对您有所帮助。我在 Scott Hanselman's blog 看到的 C# 代码(我做了一些改动)。

存储文件属性的类:

public class ViewDataUploadFilesResult
{
    public string Name { get; set; }
    public int Length { get; set; }
    public string Type { get; set; }
}

ajax调用的上传代码:

    [HttpPost]
    public string UploadFiles()
    {
        var r = new List<ViewDataUploadFilesResult>();
        Core.Settings settings = new Core.Settings();
        foreach (string file in Request.Files)
        {
            HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
            if (hpf.ContentLength == 0)
                continue;
            string savedFileName = Path.Combine(settings.StorageLocation + "\\Files\\", Path.GetFileName(hpf.FileName));
            hpf.SaveAs(savedFileName);

            r.Add(new ViewDataUploadFilesResult()
            {
                Name = hpf.FileName,
                Length = hpf.ContentLength,
                Type = hpf.ContentType
            });
        }
        return "{\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes", r[0].Length) + "\"}";
    }

神奇的 javascript 片段:

$('#file_upload').fileUploadUI({
    uploadTable: $('#files'),
    downloadTable: $('#files'),
    buildUploadRow: function (files, index) {
        return $('<tr><td>' + files[index].name + '<\/td>' +
                '<td class="file_upload_progress"><div><\/div><\/td>' +
                '<td class="file_upload_cancel">' +
                '<button class="ui-state-default ui-corner-all" title="Cancel">' +
                '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
                '<\/button><\/td><\/tr>');
    },
    buildDownloadRow: function (file) {
        return $('<tr><td>' + file.name + '<\/td><\/tr>');
    }
});

看一看并做一些测试。

--

编辑

I wrote an article about it .

关于jquery-plugins - jQuery 文件上传插件要求我下载文件,有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086189/

相关文章:

jQuery 插件 : Using callback to alter plugin behavior

javascript - 当一个 Accordion 打开而另一个关闭时,我该怎么做?

jquery-plugins - 如何激活 Twitter Bootstrap Affix 组件的链接?

asp.net-mvc-3 - 模拟 Request.QueryString 用于单元测试并针对 View 进行断言

asp.net-mvc-3 - Razor 不理解未封闭的 html 标签

c# - 从网站 ASP.NET MVC3 中提取元数据

jquery - 无法多次从 popup.js 元素检索 css 属性

javascript - 如何获取浏览器的子窗口数量

asp.net-mvc-3 - 为什么Html.BeginForm会生成空 Action ?

asp.net-mvc - asp.net MVC Controller 和身份验证