简体   繁体   English

在MVC 4中返回下载在Android上不起作用

[英]Returning Download in MVC 4 Doesn't Work on Android

I'm a first time poster so please excuse my ignorance on posting technique. 我是第一次发布海报,请原谅我对发布技术的无知。 I have been searching for hours and cannot seem to find the answer to this so hopefully the smarter people in the room can help? 我一直在寻找几个小时,似乎无法找到答案,所以希望房间里聪明的人可以帮忙吗? I have a web application written in .net MVC 4 that connects to several cloud service APIs. 我有一个用.net MVC 4编写的Web应用程序,它连接到多个云服务API。 The user can then download files from this web app. 然后,用户可以从此Web应用程序下载文件。 I've been able to get this to work on all platforms except android. 我已经能够在除android之外的所有平台上使用它。 When I try to download the file on Android it hangs and eventually fails. 当我尝试在Android上下载文件时,它会挂起并最终失败。 While downloading it has the filename but says <unknown>. 下载时有文件名但是说<unknown>。 Does anyone have a solution for this? 有人有解决方案吗? Here is the code I'm using: 这是我正在使用的代码:

if (String.IsNullOrEmpty(type))
{
    type = "application/octet-stream";
}

byte[] file = openDropBoxFile(path, filename);

if (file != null)
{                    
    return File(file, type, filename);
}

Thanks Slack Shot, sadly it didn't change the behavior on android. 谢谢Slack Shot,遗憾的是它没有改变android上的行为。 Here's how I changed the code in case I implemented your solution incorrectly: 以下是我在错误地实现您的解决方案时更改代码的方法:

if (String.IsNullOrEmpty(type))
{
    type = "application/octet-stream";
}        

byte[] file = openDropBoxFile(path, filename);
if (file != null)
{
    var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = filename,
        Inline = false,
    };
    Response.AppendHeader("Content-Disposition", cd.ToString());
    return File(file, type);
}

Some more information on the issue. 关于这个问题的更多信息。 I was able to get the download to work on Firefox for android, but not the native browser or Google Chrome for Android. 我能够将下载工作在Firefox for Android上,而不是原生浏览器或Google Chrome for Android。

Headers as reported by my last attempt, (I also tried without Length and used chunked transfer encoding instead): 我上次尝试报告的标题,(我也试过没有长度,而是使用了chunked transfer encoding):

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 45113
Content-Type: application/pdf
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename="testfile.pdf"
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 09 Nov 2013 17:55:40 GMT

UPDATE: We've solved part of the issue!! 更新:我们已经解决了部分问题!!

Turns out if we turn off our SSL the downloads work just fine. 事实证明,如果我们关闭SSL,下载工作就可以了。 Anyone run into this? 有人碰到这个吗?

You might want to try adding a Content-Disposition Header. 您可能想尝试添加Content-Disposition标头。

       var cd = new System.Net.Mime.ContentDisposition
        {
                // for example foo.bak
                FileName = "Summary.csv",

                // always prompt the user for downloading, set to true if you want 
                // the browser to try to show the file inline
                Inline = false,
        };
        Response.AppendHeader("Content-Disposition", cd.ToString());
        return File(model.SummaryDownload.Content().ToArray(), "text/csv");

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

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