如何将文件下载到远程机器,就像我在自己的机器上一样或类似?
How can I download a file to a remote machine the same or similarly to how I'm doing it to my own machine?
我正在使用此方法在 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
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.NET Web API 项目,所以我的文件系统被认为 "fair game" 用于写入。
我怎样才能在任何远程用户的机器上完成同样的事情(最好是通过前面提到的可见下载)(显然,我不能把文件放在任何地方,不仅出于安全原因,而且因为我不不知道他们可能有哪些文件夹)?
更新
我打算试试这个:
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,但这些属性或方法中的 none 是 HttpResponseMessage 环境的一部分。我什至尝试了一个原始的 "Response.Buffer" 来代替 "httprm.Buffer",希望未声明的 "Response" 对象(也没有在示例代码中声明)至少给我提供可解析性,但没有这样的意外发现在我身上。
更新 2
我将尽快对已接受的答案进行赏金;这是我得到的最有帮助的工具之一。我将这种智慧与其他点点滴滴结合起来(没有双关语意)一个提示,展示了如何保存 Excel 数据,然后从 Web API 应用程序再次读取它并下载它 here.
好吧,你似乎并没有下载任何东西,你只是对服务器的本地 C: 驱动器执行了写入操作。
要下载,您需要 return 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;
我正在使用此方法在 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
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.NET Web API 项目,所以我的文件系统被认为 "fair game" 用于写入。
我怎样才能在任何远程用户的机器上完成同样的事情(最好是通过前面提到的可见下载)(显然,我不能把文件放在任何地方,不仅出于安全原因,而且因为我不不知道他们可能有哪些文件夹)?
更新
我打算试试这个:
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,但这些属性或方法中的 none 是 HttpResponseMessage 环境的一部分。我什至尝试了一个原始的 "Response.Buffer" 来代替 "httprm.Buffer",希望未声明的 "Response" 对象(也没有在示例代码中声明)至少给我提供可解析性,但没有这样的意外发现在我身上。
更新 2
我将尽快对已接受的答案进行赏金;这是我得到的最有帮助的工具之一。我将这种智慧与其他点点滴滴结合起来(没有双关语意)一个提示,展示了如何保存 Excel 数据,然后从 Web API 应用程序再次读取它并下载它 here.
好吧,你似乎并没有下载任何东西,你只是对服务器的本地 C: 驱动器执行了写入操作。
要下载,您需要 return 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;