从 URL-asp.net 下载/Stream 文件

我需要流一个文件,这将导致保存为提示在浏览器中。 问题是,文件所在的目录实际上是映射的,因此我无法使用 Server。MapPath 来确定它的实际位置。该目录不在与网站相同的位置(甚至不在活动框中的物理服务器上)。

我想像下面这样的东西,但这将允许我传递一个网络 URL,而不是一个服务器文件路径。

我可能不得不最终从一个配置基路径构建我的文件路径,然后附加到路径的其余部分,但希望我可以这样做。

var filePath = Server.MapPath(DOCUMENT_PATH);


if (!File.Exists(filePath))
return;


var fileInfo = new System.IO.FileInfo(filePath);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(filePath);
Response.End();
337900 次浏览

您可以尝试使用带有 IIS 路径前缀的 DirectoryEntry 类:

using(DirectoryEntry de = new DirectoryEntry("IIS://Localhost/w3svc/1/root" + DOCUMENT_PATH))
{
filePath = de.Properties["Path"].Value;
}


if (!File.Exists(filePath))
return;


var fileInfo = new System.IO.FileInfo(filePath);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(filePath);
Response.End();

您可以使用 HttpWebRequest 获取文件并将其流回客户端。这允许您获取带有 URL 的文件。我发现的一个例子(但不记得在哪里给信用)是

    //Create a stream for the file
Stream stream = null;


//This controls how many bytes to read at a time and send to the client
int bytesToRead = 10000;


// Buffer to read bytes in chunk size specified above
byte[] buffer = new Byte[bytesToRead];


// The number of bytes read
try
{
//Create a WebRequest to get the file
HttpWebRequest fileReq = (HttpWebRequest) HttpWebRequest.Create(url);


//Create a response for this request
HttpWebResponse fileResp = (HttpWebResponse) fileReq.GetResponse();


if (fileReq.ContentLength > 0)
fileResp.ContentLength = fileReq.ContentLength;


//Get the Stream returned from the response
stream = fileResp.GetResponseStream();


// prepare the response to the client. resp is the client Response
var resp = HttpContext.Current.Response;


//Indicate the type of data being sent
resp.ContentType = MediaTypeNames.Application.Octet;
    

//Name the file
resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
    

int length;
do
{
// Verify that the client is connected.
if (resp.IsClientConnected)
{
// Read data into the buffer.
length = stream.Read(buffer, 0, bytesToRead);


// and write it out to the response's output stream
resp.OutputStream.Write(buffer, 0, length);


// Flush the data
resp.Flush();


//Clear the buffer
buffer = new Byte[bytesToRead];
}
else
{
// cancel the download if client has disconnected
length = -1;
}
} while (length > 0); //Repeat until no data is read
}
finally
{
if (stream != null)
{
//Close the input stream
stream.Close();
}
}

两年后,我使用了达拉斯的答案,但我不得不将 HttpWebRequest改为 FileWebRequest,因为我链接的是直接文件。不知道是不是所有地方都是这样,但我觉得我应该加上它。还有,我把

var resp = Http.Current.Resonse

只是在引用 resp的地方使用 Http.Current.Response

我经常这样做,我想我可以添加一个更简单的答案。我在这里将它设置为一个简单的类,但是我每天晚上运行它来收集我关注的公司的财务数据。

class WebPage
{
public static string Get(string uri)
{
string results = "N/A";


try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();


StreamReader sr = new StreamReader(resp.GetResponseStream());
results = sr.ReadToEnd();
sr.Close();
}
catch (Exception ex)
{
results = ex.Message;
}
return results;
}
}

在这种情况下,我传入一个 url,它将页面作为 HTML 返回。如果您想对流做一些不同的事情,您可以轻松地改变这一点。

你可以这样使用:

string page = WebPage.Get("http://finance.yahoo.com/q?s=yhoo");

将 url 下载到字节并将字节转换为 stream:

using (var client = new WebClient())
{
var content = client.DownloadData(url);
using (var stream = new MemoryStream(content))
{
...
}
}

如果我们在 Citrix Netscaler 上使用负载均衡器(没有 WAF 策略) ,达拉斯接受的解决方案对我们是有效的。

当文件与 WAF 关联时,下载的文件不能通过 Netscaler 的 LB 工作,因为当前场景(内容长度不正确)是 RFC 违规,而 AppFW 重置了连接,当 WAF 策略没有关联时,这种情况不会发生。

所以缺少的是:

回应

参见: 试图使用 asp.net 传输一个 PDF 文件会产生一个“损坏的文件”

如果你正在寻找一个.NET 核心版本的@Dallas 的答案,使用下面的。

        Stream stream = null;


//This controls how many bytes to read at a time and send to the client
int bytesToRead = 10000;


// Buffer to read bytes in chunk size specified above
byte[] buffer = new Byte[bytesToRead];


// The number of bytes read
try
{
//Create a WebRequest to get the file
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(@"file url");


//Create a response for this request
HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();


if (fileReq.ContentLength > 0)
fileResp.ContentLength = fileReq.ContentLength;


//Get the Stream returned from the response
stream = fileResp.GetResponseStream();


// prepare the response to the client. resp is the client Response
var resp = HttpContext.Response;


//Indicate the type of data being sent
resp.ContentType = "application/octet-stream";


//Name the file
resp.Headers.Add("Content-Disposition", "attachment; filename=test.zip");
resp.Headers.Add("Content-Length", fileResp.ContentLength.ToString());


int length;
do
{
// Verify that the client is connected.
if (!HttpContext.RequestAborted.IsCancellationRequested)
{
// Read data into the buffer.
length = stream.Read(buffer, 0, bytesToRead);


// and write it out to the response's output stream
resp.Body.Write(buffer, 0, length);




//Clear the buffer
buffer = new Byte[bytesToRead];
}
else
{
// cancel the download if client has disconnected
length = -1;
}
} while (length > 0); //Repeat until no data is read
}
finally
{
if (stream != null)
{
//Close the input stream
stream.Close();
}
}

我认为在.Net Core 中实现这一点的最简单方法是:

using (MemoryStream ms = new MemoryStream()) {
new HttpClient().GetStreamAsync(webPath).Result.CopyTo(ms);
  

// use ms in what you want
}

现在你有文件下载作为流内的 ms。