从网址下载/流文件 - asp.net [英] Download/Stream file from URL - asp.net
问题描述
我要流的文件,这将导致保存在浏览器提示。
问题是,该文件所在的目录被虚拟地映射,所以我无法使用来使用Server.Mappath确定它的实际位置。该目录是不相同的位置(或在活框甚至phyical服务器)作为网站
我想是这样的追随者,但是,让我通过一个Web URL,而不是服务器文件的路径。
我可能要结束了从配置基本路径建设我的文件路径,然后在路径的其余部分追加,但我希望我能做到这一点,而不是这样的。
VAR文件路径=使用Server.Mappath(DOCUMENT_PATH);如果(!File.Exists(文件路径))
返回;VAR的fileInfo =新System.IO.FileInfo(文件路径);
Response.ContentType =应用程序/八位字节流;
Response.AddHeader(内容处置的String.Format(附件;文件名= \\{0} \\,文件路径));
Response.AddHeader(内容长度,fileInfo.Length.ToString());
Response.WriteFile(文件路径);
到Response.End();
您可以使用的HttpWebRequest获取文件和流回给客户端。这可以让你得到一个URL文件。这是我发现的(但不记得在哪里给予信贷)的一个例子是
//创建该文件的流
流流= NULL; //这个控制多少字节一次读取和发送到客户端
INT bytesToRead = 10000; //缓冲区读取上面指定的块大小的字节
字节[]缓冲区=新的字节[bytesToRead] //读取的字节数
尝试
{
//创建一个WebRequest的获取文件
HttpWebRequest的fileReq =(HttpWebRequest的)HttpWebRequest.Create(URL); //创建此请求的响应
HttpWebResponse fileResp =(HttpWebResponse)fileReq.GetResponse(); 如果(fileReq.ContentLength大于0)
fileResp.ContentLength = fileReq.ContentLength; //获取数据从响应返回
流= fileResp.GetResponseStream(); // prepare响应给客户机。 RESP是客户端的响应
VAR RESP = HttpContext.Current.Response; //指示数据的类型被发送
resp.ContentType =应用程序/八位字节流; //命名该文件
resp.AddHeader(内容处置,附件;文件名= \\+文件名+\\);
resp.AddHeader(内容长度,fileResp.ContentLength.ToString()); INT长;
做
{
//验证客户端连接。
如果(resp.IsClientConnected)
{
//读取数据到缓冲区。
长度= stream.Read(缓冲液,0,bytesToRead); //并写入到响应的输出流
resp.OutputStream.Write(缓冲液,0,长度); //刷新数据
resp.Flush(); //清除缓冲区
缓冲区=新的字节[bytesToRead]
}
其他
{
//取消下载客户端是否已断开连接
长度= -1;
}
}而(长度大于0); //重复,直到没有数据被读取
}
最后
{
如果(流!= NULL)
{
//关闭输入流
stream.Close();
}
}
I need to stream a file which will result in save as prompt in the browser. The issue is, the directory that the file is located is virtually mapped, so I am unable to use Server.MapPath to determine it's actual location. The directory is not in the same location (or even phyical server on the live boxes) as the website.
I'd like something like the following, but that will allow me to pass a web URL, and not a server file path.
I may have to end up building my file path from a config base path, and then append on the rest of the path, but hopefully I can do it this way instead.
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();
You could use HttpWebRequest to get the file and stream it back to the client. This allows you to get the file with a url. An example of this that I found ( but can't remember where to give credit ) is
//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();
}
}
这篇关于从网址下载/流文件 - asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!