4

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

我首先尝试了从 URL - asp.net 下载/流文件的方法。但这不起作用,因为我所在的班级是从 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”。

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

ETA 我之前的尝试:

    //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: Server cannot set content type after HTTP headers have been sent. 这对我来说没有意义,因为我在附加标头之前设置了内容类型。

堆栈跟踪是:

[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对象。我仍然遇到同样的异常。我无法解释为什么普遍接受的方法向我抱怨已经发送了响应,或者为什么我的代码不会提示用户下载文件。

4

2 回答 2

0

只需在之后移动所有内容类型和标题代码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();
  }
}
于 2013-09-11T05:54:23.017 回答
0

在 ASPX<%@ Page...%>指令中设置 ContentType 属性:

<%@ Page ContentType="application/zip" %>
于 2013-09-12T05:56:28.993 回答