gpt4 book ai didi

c# - 下载动态文件

转载 作者:行者123 更新时间:2023-11-30 13:01:35 25 4
gpt4 key购买 nike

我正在我的网络应用程序中动态创建一个文件。我想将此文件发送给用户,就像许多网站在您下载文件时所做的那样。

我首先在 Download/Stream file from URL - asp.net 尝试了这种方法.但这没有用,因为我所在的类(class)是从 ASPX 网页中删除的一层;我的结果是文件覆盖了自己,交替出现我无法创建正确的请求/响应对象。

然后我尝试了这个方法:

try
{

WebClient myClient = new WebClient();
string basefile = Path.GetFileName( file_name );
myClient.DownloadFile( file_name, basefile );
}
catch (WebException we)
{
string message = we.Message;
}

这引发了一个异常:访问路径 'C:\Program Files (x86)\IIS Express\file-being-sent.ext' 被拒绝。

那么,我该如何流式传输文件呢?

预计我之前的尝试:

    //Create a WebRequest to get the file
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create( url ); //Send request to this 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 = new Stream();
writer = new StreamWriter( file_name );
//stream = writer.BaseStream.Length


// 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";
resp.ContentType = "application/zip";

//Name the file
resp.AddHeader( "Content-Disposition", "attachment; filename=\"" + Path.GetFileName( file_name ) + "\"" );
resp.AddHeader( "Content-Length", writer.BaseStream.Length.ToString() );
// Verify that the client is connected.

if (resp.IsClientConnected)
{
resp.Write( writer );
resp.Flush();
}

我还没有弄清楚要将什么传递给 HttpCreateRequest() 的调用。

ETA2:这是我目前正在使用的。各种 SOF 帖子说这应该通过“打开或保存”对话框提示用户,但对我来说仍然没有。

StreamReader reader = new StreamReader( file_name );
var resp = HttpContext.Current.Response;

//Indicate the type of data being sent
resp.ContentType = "application/zip";
resp.AppendHeader( "Content-Disposition", "attachment; filename=\"" + Path.GetFileName( file_name ) + "\"" );
resp.AppendHeader( "Content-Length", reader.BaseStream.Length.ToString() );
// Verify that the client is connected.

if (resp.IsClientConnected)
{
resp.Clear();
resp.TransmitFile( file_name ); //does not buffer into memory, therefore scales better for large files and heavy usage
resp.Flush();
}

ETA3:IIS 跟踪日志报告异常,System.Web.HttpException:发送 HTTP header 后服务器无法设置内容类型。 这对我来说没有意义,因为我在附加标题之前设置内容类型。

堆栈跟踪是:

[HttpException (0x80004005): Server cannot set content type after HTTP headers have been sent.]
System.Web.HttpResponse.set_ContentType(String value) +9681570
System.Web.HttpResponseInternalWrapper.set_ContentType(String value) +41
System.Web.UI.PageRequestManager.RenderPageCallback(HtmlTextWriter writer, Control pageControl) +139
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +268
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1386

ETA4:我将流式传输移动到 ASPX 页面的代码隐藏文件中,该文件可以直接访问 Response 对象。我仍然遇到同样的异常。我无法解释为什么普遍接受的方法向我提示响应已经发送,或者为什么我的代码不会提示用户下载文件。

最佳答案

只需在 resp.Clear() 之后移动所有内容类型和标题代码

using (StreamReader reader = new StreamReader( file_name ))
{
var resp = HttpContext.Current.Response;
resp.BufferOutput = true; //this is from the SO article
//resp.Buffer = True; //this is from msdn comments

// Verify that the client is connected.

if (resp.IsClientConnected)
{
resp.Clear();
resp.ClearHeaders();
//Indicate the type of data being sent
resp.ContentType = "application/zip";
resp.AppendHeader( "Content-Disposition", "attachment; filename=\"" + Path.GetFileName( file_name ) + "\"" );
resp.AppendHeader( "Content-Length", reader.BaseStream.Length.ToString() );
resp.TransmitFile( file_name ); //does not buffer into memory, therefore scales better for large files and heavy usage
resp.End();
}
}

关于c# - 下载动态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18599735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com