mp3*_*uck 66 c# asp.net streaming file-io
我需要流式传输一个文件,这将导致在浏览器中保存为提示.问题是,文件所在的目录是虚拟映射的,因此我无法使用Server.MapPath来确定它的实际位置.该目录与网站不在同一位置(甚至在实时盒上的物理服务器上).
我想要像下面这样的东西,但这将允许我传递一个Web 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();
Run Code Online (Sandbox Code Playgroud)
Dal*_*las 99
您可以使用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 = "application/octet-stream";
//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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 23
将url下载到字节并将字节转换为流:
using (var client = new WebClient())
{
var content = client.DownloadData(url);
using (var stream = new MemoryStream(content))
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
Cod*_*ops 12
我这样做了很多,并认为我可以添加一个更简单的答案.我在这里将它设置为一个简单的类,但我每天晚上运行它来收集我正在关注的公司的财务数据.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我传入一个URL,它将页面作为HTML返回.如果你想对流做一些不同的事情,你可以轻松地改变它.
你这样使用它:
string page = WebPage.Get("http://finance.yahoo.com/q?s=yhoo");
Run Code Online (Sandbox Code Playgroud)
小智 7
如果您正在寻找 @Dallas 答案的 .NET Core 版本,请使用以下内容。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
2年后,我用了达拉斯小牛队的答案,但我不得不改变HttpWebRequest对FileWebRequest,因为我是链接到直接文件。不确定是否到处都是这种情况,但我想我会添加它。另外,我删除了
var resp = Http.Current.Resonse
并Http.Current.Response在任何resp被引用的地方使用。
我认为在 .Net Core 中执行此操作的最简单方法是:
using (MemoryStream ms = new MemoryStream())
using (HttpClient client = new HttpClient())
{
client.GetStreamAsync(url).Result.CopyTo(ms);
// use ms in what you want
}
}
Run Code Online (Sandbox Code Playgroud)
现在您已将文件作为流下载到 ms 中。
| 归档时间: |
|
| 查看次数: |
262372 次 |
| 最近记录: |