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

Ray*_*Ray 2 jquery-plugins asp.net-mvc-3

我正在使用https://github.com/blueimp/jQuery-File-Upload,我能够将文件上传并保存到指定的文件夹,然后我返回Json对象.然后浏览器(我使用IE8)弹出"文件下载"对话框,并要求我下载一个名为"upload75bea5a4"的文件,没有扩展名.我只是弄清楚出了什么问题?

Dav*_*usa 7

我正在使用相同的插件,它对我没有任何问题.我会发布我正在使用的代码,这样可以帮助你.我在Scott Hanselman博客上看到的C#代码(我做了一些改动).

用于存储文件属性的类:

public class ViewDataUploadFilesResult
{
    public string Name { get; set; }
    public int Length { get; set; }
    public string Type { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

上传代码,由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) + "\"}";
    }
Run Code Online (Sandbox Code Playgroud)

制作魔法的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>');
    }
});
Run Code Online (Sandbox Code Playgroud)

看看并做一些测试.

-

编辑

我写了一篇关于它的文章.

  • 也许形式... <form id ="file_upload"action ="/ Post/UploadFiles"method ="post"**enctype ="multipart/form-data"**> (2认同)