Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我需要将文件从服务器发送到浏览器(web客户端)_C#_Asp.net - Fatal编程技术网

C# 我需要将文件从服务器发送到浏览器(web客户端)

C# 我需要将文件从服务器发送到浏览器(web客户端),c#,asp.net,C#,Asp.net,我需要将文件从服务器发送到浏览器web客户端c ASP.NET 但在下载文件的同时,程序继续执行。 我尝试使用任务、线程、异步方法 //Create a WebRequest to get the file FileWebRequest fileReq = (FileWebRequest)FileWebRequest.Create(this.CaminhoCompleto); //Create a response for this request FileWebResponse fileRe

我需要将文件从服务器发送到浏览器web客户端c ASP.NET 但在下载文件的同时,程序继续执行。 我尝试使用任务、线程、异步方法

//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);

使用Iframe从服务器请求文件。您可以使用JavaScript创建一个,并将其src作为服务器端点。当内容准备好下载时,它将从浏览器中打开一个保存对话框。

该文件将在HttpResponse中返回,该文件仅在控制器执行完毕后发送。。。如果处理对响应不重要,您可以启动一个不会阻止响应发送的新线程。为什么使用FileWebRequest而不仅仅是普通的FileStream?我想使用这个异步线程为什么它需要异步?只有当所有处理完成时才会发送响应,除非您在其他线程上执行处理。因为用户界面必须继续运行下载。ASP.NET