C# 从 webservice 下载文件 - 在 ASP.NET 站点中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2239623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download file from webservice - in ASP.NET site
提问by user210757
I want to push a file to the browser from a website using a webservice. I'm currently reading the file into a base64 byte array, and returning that from the webservice. This webservice is called from a website, and i'm stuck on how to push this as the original file to the browser. Ideally I would like to read the byte array into a memory stream, and then just write it to the Response stream if possible so the end user just downloads the file.
我想使用网络服务将文件从网站推送到浏览器。我目前正在将文件读入 base64 字节数组,然后从 web 服务返回。这个网络服务是从一个网站调用的,我被困在如何将它作为原始文件推送到浏览器上。理想情况下,我想将字节数组读入内存流,然后尽可能将其写入响应流,以便最终用户下载文件。
采纳答案by Randolpho
First, rather than send a base64 byte array, have your web service simply return a byte array for your file. Response.OutputStream.Write()
will automatically base64 encode your bytes, so you might as well have them un-encoded in your memory stream.
首先,不是发送 base64 字节数组,而是让您的 Web 服务简单地为您的文件返回一个字节数组。Response.OutputStream.Write()
将自动对您的字节进行 base64 编码,因此您最好将它们未编码到您的内存流中。
Second, you'll need more than just the bytes. You'll need meta-data associated with the file. For the snippet below, I've placed all of that metadata into a separate class (local instance named "file"). Then, just use this snippet, once you have the data you need:
其次,您需要的不仅仅是字节。您将需要与文件关联的元数据。对于下面的代码片段,我已将所有元数据放入一个单独的类(名为“file”的本地实例)中。然后,一旦您拥有所需的数据,只需使用此代码段:
Response.Clear();
Response.ClearHeaders();
Response.ContentType = file.ContentType;
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.FileName + "\"");
Response.AddHeader("Content-Length", file.FileSize.ToString());
Response.OutputStream.Write(file.Bytes, 0, file.Bytes.Length);
Response.Flush();
Response.End();
回答by Derek Slager
It's possible, you'll need to make sure you explicitly set the ContentType of the HttpResponse, for example:
有可能,您需要确保显式设置 HttpResponse 的 ContentType,例如:
Response.ContentType = "image/jpeg";
Response.OutputStream.Write(buffer, 0, buffer.Length);
If you want to control the file name, you'll have to add a Content-Disposition header. Google can help you find the right way to sort that out.
如果要控制文件名,则必须添加 Content-Disposition 标头。Google 可以帮助您找到解决问题的正确方法。
回答by Mark Milbourne
It really depends on the interface to your webservice. Ie SOAP, REST, ASPX.
这实际上取决于您的网络服务的接口。即 SOAP、REST、ASPX。
One thing you can try is to change the content-type in your Response to "Application/octet-stream". Or something similar to tell the receiver the MIME type.
您可以尝试的一件事是将响应中的内容类型更改为“应用程序/八位字节流”。或者类似的东西告诉接收者 MIME 类型。
If your using WCF rest you can use Stream as a return type on the web service.
如果您使用 WCF rest,您可以使用 Stream 作为 Web 服务的返回类型。
回答by mythz
Its usually a bad idea to embed a file in a web service. You just add overhead and complexity with no real benefit.
在 Web 服务中嵌入文件通常是个坏主意。您只是增加了开销和复杂性而没有真正的好处。
Instead you should provide a IHttpHandler to handle the file upload. Most web servers also provide helper API's to simplify this, e.g. in ASP.NET you can access the uploaded file with:
相反,您应该提供一个 IHttpHandler 来处理文件上传。大多数 Web 服务器还提供帮助程序 API 来简化此操作,例如在 ASP.NET 中,您可以通过以下方式访问上传的文件:
HttpContext.Request.Files[0]
HttpContext.Request.Files[0]
There are plenty of javascript file upload scripts that simplify this on the client: http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
有很多 javascript 文件上传脚本可以在客户端简化此操作:http: //www.phpletter.com/Demo/AjaxFileUpload-Demo/