简体   繁体   English

如何在文件准备好下载之前显示下载对话框?

[英]How to show download dialog before file is ready to download?

I'm getting a file from a database in byte [] format and want user to see download dialog before Linq will take it from the database.我从数据库中获取字节 [] 格式的文件,并希望用户在 Linq 从数据库中获取它之前看到下载对话框。 It's in C# and ASP.NET.它位于 C# 和 ASP.NET 中。

Now, it's like this:现在,它是这样的:

  1. User choose a file, click on it.用户选择一个文件,点击它。
  2. In code I get id of file clicked and using Linq I'm downloading.在代码中,我得到了点击文件的 id 并使用 Linq 我正在下载。
  3. Then I send the file by Response.OutputStream.Write(content, 0, content.Length);然后我通过 Response.OutputStream.Write(content, 0, content.Length); 发送文件
  4. Before a file is downloaded from the database user won't see any download dialog.在从数据库下载文件之前,用户不会看到任何下载对话框。

What can I do if I want users to see the download dialog before file is downloaded?如果我希望用户在下载文件之前看到下载对话框,我该怎么办?

Code:代码:
Getting file by id:通过 id 获取文件:

public static byte[] getFile(Guid id)
{
    var linqFile =  from file in MyDB.Files
                    where file.IdPliku.Equals(id)
                    select new
                    {
                        Content = file.Content
                    };

     return linqFile.ToList().FirstOrDefault().Content.ToArray();
}

Saving file:保存文件:

public void SaveFile(Guid fileID, string filename, string mimeTypes)
{
    try
    {
        byte[] content = FileService.getFile(fileID);
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = mimeTypes;
        Response.AppendHeader("Accept-Ranges", "bytes");
        Response.AppendHeader("Content-Range", string.Format("0-{0}/{1}", content.Length, content.Length));
        Response.AppendHeader("Content-Length", content.Length.ToString());
        Response.AppendHeader("Content-Encoding", "utf-8");
        Response.AppendHeader("Content-Type", Response.ContentType);
        Response.AppendHeader("Content-Disposition", "attachment; filename= " + HttpUtility.UrlEncode(filename));
        Response.OutputStream.Write(content, 0, content.Length);
        //Response.BinaryWrite(content);
        Response.Flush();
    }
    finally
    {
        Response.Close();
    }
}

You are my hope.你是我的希望。

your issue is here:你的问题在这里:

byte[] content = FileService.getFile(fileID);

because in this line you allocate the whole file in the web server's RAM and put everything in there, all content of the file from the database;因为在这一行中,您将整个文件分配到 web 服务器的 RAM 中,并将所有内容放在那里,文件的所有内容都来自数据库; what happens later does not matter anymore because you have already downloaded from db to web server in this line!!!以后发生的事情不再重要,因为您已经在这一行中从 db 下载到 web 服务器!

I am having such Deja-vu because I am sure I have given exactly the same comment on a very same question few weeks ago.我有这种似曾相识的感觉,因为我确信几周前我对一个非常相同的问题给出了完全相同的评论。 Can't find it now, search for something like this here in SO.现在找不到,在 SO 中搜索类似的内容。

In fact the solution is to stream directly to the output stream of the Response avoiding your byte[] array allocation above, to get this your data layer should of course support it and if it does not you could add a method for this.事实上,解决方案是 stream 直接到 output stream 的响应避免你的 byte[] 数组分配的方法,如果它不应该添加一个方法来支持它。 You want to use SQL Server filestream or something similar.您想使用 SQL 服务器文件流或类似的东西。

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

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