首页 > 解决方案 > 将 Response.OutputStream 保存到文件夹

问题描述

我有一个HttpWebRequest当前正在下载文件,但我想停止下载并将文件保存在文件夹中。

webRequest.Method = "GET";
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.ContentType = "text/xml;charset=UTF-8";
using (var webResponse = webRequest.GetResponse())
using (var webStream = webResponse.GetResponseStream())
{
    if (webStream != null)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=\"test.pdf\"");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        webStream.CopyTo(Response.OutputStream);
        Response.Flush();
        Response.End();
    }
}

我尝试将 ContactType 更改为Response.ContentType = "application/x-download";并添加Response.TransmitFile,但文件仍在下载。

标签: c#asp.nethttpwebrequesthttpwebresponse

解决方案


推荐阅读