ASMX 文件下载

发布于 2024-10-14 09:01:59 字数 795 浏览 1 评论 0原文

我有一个 ASMX(无 WCF)Web 服务,其方法可响应如下所示的文件:

[WebMethod]
public void GetFile(string filename)
{
    var response = Context.Response;
    response.ContentType = "application/octet-stream";
    response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    using (FileStream fs = new FileStream(Path.Combine(HttpContext.Current.Server.MapPath("~/"), fileName), FileMode.Open))
    {
        Byte[] buffer = new Byte[256];
        Int32 readed = 0;

        while ((readed = fs.Read(buffer, 0, buffer.Length)) > 0)
        {
            response.OutputStream.Write(buffer, 0, readed);
            response.Flush();
        }
    }
}

我想使用控制台应用程序中的 Web 引用将此文件下载到本地文件系统。如何获取文件流?

PS 我尝试通过发布请求(使用 HttpWebRequest 类)下载文件,但我认为还有更优雅的解决方案。

I have an ASMX(no WCF) webservice with a method that responses a file that looks like:

[WebMethod]
public void GetFile(string filename)
{
    var response = Context.Response;
    response.ContentType = "application/octet-stream";
    response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    using (FileStream fs = new FileStream(Path.Combine(HttpContext.Current.Server.MapPath("~/"), fileName), FileMode.Open))
    {
        Byte[] buffer = new Byte[256];
        Int32 readed = 0;

        while ((readed = fs.Read(buffer, 0, buffer.Length)) > 0)
        {
            response.OutputStream.Write(buffer, 0, readed);
            response.Flush();
        }
    }
}

and I want to download this file to local filesystem using web reference in my console application. How to get the filestream?

P.S. I tried download files via post request(using HttpWebRequest class) but I think there is much more elegant solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-10-21 09:01:59

您可以在 Web 服务的 web.config 中启用 HTTP。

    <webServices>
        <protocols>
            <add name="HttpGet"/>
        </protocols>
    </webServices>

然后您应该能够仅使用 Web 客户端下载该文件(使用文本文件进行测试):

string fileName = "bar.txt"
string url = "http://localhost/Foo.asmx/GetFile?filename="+fileName;
using(WebClient wc = new WebClient())
wc.DownloadFile(url, @"C:\bar.txt");

编辑:

要支持设置和检索 cookie,您需要编写自定义 WebClient 重写 GetWebRequest()< 的类/code>,这很容易做到,只需几行代码:

public class CookieMonsterWebClient : WebClient
{
    public CookieContainer Cookies { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = Cookies;
        return request;
    }
}

要使用此自定义 Web 客户端,您需要执行以下操作:

myCookieContainer = ... // your cookies

using(CookieMonsterWebClient wc = new CookieMonsterWebClient())
{
    wc.Cookies = myCookieContainer; //yum yum
    wc.DownloadFile(url, @"C:\bar.txt");
}

You can enable HTTP in the web.config of your web service.

    <webServices>
        <protocols>
            <add name="HttpGet"/>
        </protocols>
    </webServices>

Then you should be able to just use a web client to download the file (tested with text file):

string fileName = "bar.txt"
string url = "http://localhost/Foo.asmx/GetFile?filename="+fileName;
using(WebClient wc = new WebClient())
wc.DownloadFile(url, @"C:\bar.txt");

Edit:

To support setting and retrieving cookies you need to write a custom WebClient class that overrides GetWebRequest(), it's easy to do and just a few lines of code:

public class CookieMonsterWebClient : WebClient
{
    public CookieContainer Cookies { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = Cookies;
        return request;
    }
}

To use this custom web client you would do:

myCookieContainer = ... // your cookies

using(CookieMonsterWebClient wc = new CookieMonsterWebClient())
{
    wc.Cookies = myCookieContainer; //yum yum
    wc.DownloadFile(url, @"C:\bar.txt");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文