jQuery的文件上传插件,问我要下载的文件,有什么不好? [英] jQuery File Upload plugin asks me to download the file, what is wrong?

查看:120
本文介绍了jQuery的文件上传插件,问我要下载的文件,有什么不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 https://github.com/blueimp/jQuery-File-Upload ,我能够上传和保存文件到指定的文件夹,然后我返回JSON对象。然后在浏览器(我用IE8)弹出文件下载对话框,并要求我下载一个名为upload75bea5a4不带扩展名的文件。我只是无法弄清楚什么是错的?

I'm using https://github.com/blueimp/jQuery-File-Upload and I was able to upload and save the file to designated folder, and then I return the Json object. Then the browser (I use IE8) pops "File Download" dialog and asks me to download a file named "upload75bea5a4" with no extension. I just could not figure out what is wrong?

推荐答案

我使用的是相同的插件,它的工作不适合我任何问题。我将发布C $ CS我使用,这样可以帮助你的$。在C#code我在看到<一个href=\"http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx\"相对=nofollow>斯科特Hanselman的博客(和我做了一些改动)。

I am using the same plugin and it's working without any problems for me. I will post the codes I am using so that can help you. The C# code I saw at Scott Hanselman's blog (and I made few changes).

一个类来存储文件的属性:

A class to store the properties of the file:

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

上传code,这是由AJAX叫:

The upload code, which is called by the 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的一块,这使得魔术:

The javascript piece which makes the magic:

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

看一看,做一些测试。

Take a look and make some tests.

-

修改

我写了一篇关于它的的。

这篇关于jQuery的文件上传插件,问我要下载的文件,有什么不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆