我需要将文件从服务器发送到浏览器(Web客户端) [英] I need send a file from server to browser (web client)
本文介绍了我需要将文件从服务器发送到浏览器(Web客户端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
但是在下载文件时,程序将继续执行.
But while download file, the program continue execution.
//Create a WebRequest to get the file
FileWebRequest fileReq = (FileWebRequest)FileWebRequest.Create(this.CaminhoCompleto);
//Create a response for this request
FileWebResponse fileResp = (FileWebResponse)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
HttpResponse resp = HttpContext.Current.Response;
//ERRO!!!
teste(stream, resp, fileResp);
string nomeArq = fileResp.ResponseUri.Segments[(fileResp.ResponseUri.Segments.Length) - 1];
//Indicate the type of data being sent
resp.ContentType = "application/octet-stream";
//Name the file
resp.AddHeader("Content-Disposition", "attachment; filename=\"" + nomeArq + "\"");
resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
//await stream.CopyToAsync(resp.OutputStream);
推荐答案
在下面查看我的代码:
Review my code below:
protected void btnDowmLoad_Click(object sender, EventArgs e)
{
try
{
string strURL=txtFileName.Text;
WebClient req=new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer= true;
response.AddHeader("Content-Disposition","attachment;filename=\"" + Server.MapPath(strURL) + "\"");
byte[] data=req.DownloadData(Server.MapPath(strURL));
response.BinaryWrite(data);
response.End();
}
catch(Exception ex)
{
}
}
这篇关于我需要将文件从服务器发送到浏览器(Web客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文