代码之家  ›  专栏  ›  技术社区  ›  B. Clay Shannon-B. Crow Raven

如何将文件下载到远程计算机,与我在自己的计算机上下载文件的方式相同或相似?

  •  8
  • B. Clay Shannon-B. Crow Raven  · 技术社区  · 9 年前

    我正在使用以下方法在Web API项目中下载Excel文件(在Winforms应用程序中动态创建并保存到数据库):

    [Route("api/deliveryperformance/{unit}/{begindate}/{enddate}")]
    public HttpResponseMessage Get(string unit, string begindate, string enddate)
    {
        // adapted the first part of this code from http://stackoverflow.com/questions/11176066/how-do-i-insert-retrieve-excel-files-to-varbinarymax-column-in-sql-server-2008
        byte[] excelContents;
    
        string selectStmt = "SELECT BinaryData FROM ReportsGenerated WHERE FileBaseName = @fileBaseName";
        string fbn = string.Format("deliveryperformance/{0}/{1}/{2}", unit, begindate, enddate);
        using (SqlConnection connection = new SqlConnection(ProActWebReportsConstsAndUtils.CPSConnStr))
        using (SqlCommand cmdSelect = new SqlCommand(selectStmt, connection))
        {
            cmdSelect.Parameters.Add("@fileBaseName", SqlDbType.VarChar).Value = fbn;
    
            connection.Open();
            excelContents = (byte[])cmdSelect.ExecuteScalar();
            connection.Close();
        }
        string excelFileName = "C:\\Misc\\TestFile2.xlsx";
        File.WriteAllBytes(excelFileName, excelContents);
    
        String HtmlToDisplay = GetDownloadSuccessMessage(excelFileName);
        return new HttpResponseMessage()
        {
            Content = new StringContent(
                HtmlToDisplay,
                Encoding.UTF8,
                "text/html"
            )
        };
    }
    
    internal static string GetDownloadSuccessMessage(string excelFileName)
    {
        return string.Format("<h1>Excel spreadsheed downloaded to {0}</h1>", excelFileName);
    }
    

    这工作得很好(除了没有可见的下载操作,例如文件的图标下降到任务栏,这是从Internet下载文件时的正常情况-文件只会在指定的位置结束)。

    我的假设是,这只会因为我正在运行ASP而起作用。NETWebAPI项目,所以我的文件系统被认为是“公平的游戏”。

    我怎样才能在任何远程用户的机器上完成同样的事情(最好是前面提到的可见下载)(显然,我不能把文件放在任何地方,不仅是出于安全原因,而且因为我不知道他们可能有哪些文件夹)?

    更新

    我打算试试这个:

    HttpResponseMessage httprm = new HttpResponseMessage();
    httprm.Buffer = true;
    httprm.Charset = "";
    httprm.Cache.SetCacheability(HttpCacheability.NoCache);
    httprm.ContentType = "application/vnd.ms-excel";
    httprm.AddHeader("content-disposition", "attachment;filename=\"Funk49.xlsx\"");
    httprm.BinaryWrite(bytes);
    httprm.Flush();
    httprm.End();
    

    …改编自 here ,但这些属性或方法都不是HttpResponseMessage的一部分。我甚至尝试了一个原始的“Response.Buffer”来代替“httprm.Buffer),希望未声明的“Respense”对象(在示例代码中也没有声明)至少能为我提供可解析性,但我没有发现这样的意外。

    更新2

    我将尽快奖励接受的答案;这是我收到的最有帮助的一封信。我将这种智慧与其他零碎的东西结合起来,编译了一个技巧(不是双关语),展示了如何保存Excel数据,然后从Web API应用程序中再次读取并下载它 here .

    1 回复  |  直到 9 年前
        1
  •  5
  •   Alex K.    9 年前

    嗯,你似乎没有下载任何东西,你只需要对服务器本地的C:驱动器执行写操作。

    要下载,您将返回 excelContents 缓冲区而不是当前的HTML字符串,例如。

    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new ByteArrayContent(excelContents);
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    result.Content.Headers.ContentDisposition.FileName = "blah.xlsx";
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
    return result;