如何在 ASP.NET 中下载文件并立即将其提供给客户端?

发布于 2024-07-25 08:58:31 字数 934 浏览 9 评论 0原文

基本上我需要从需要 Windows 身份验证的位置提供文件。 我不想让我的客户直接处理它,而是想实现一个过程,以便他们可以简单地下载文件,就像他们在我的服务器上一样,当然,在他们登录到我的系统之后。 这是我到目前为止所拥有的,似乎无法正常工作:

// Create the request
WebRequest request = HttpWebRequest.Create(button.CommandArgument);
request.Credentials = new NetworkCredential(_username,_password);


// Get the response
WebResponse response = request.GetResponse();
StreamReader responseStream = new StreamReader( response.GetResponseStream());

// Send the response directly to output
Response.ContentEncoding = responseStream.CurrentEncoding;
Response.ContentType = request.ContentType;
Response.Write(responseStream.ReadToEnd());
Response.End();

当我尝试此操作时,我可以查看文件,但编码或内容类型有问题,例如,PDF 将包含 16空白页(而不是 16 页文本)。

知道我错过了什么吗?

如果有更好的方式来表达此问题,请随时更改此问题的标题

更新: 尝试了下面的两个回应,但没有运气。 我现在认为内容类型和编码都可以,但身份验证可能失败? 内容长度比实际应有的长度小很多...我是否使用了错误的 Windows 身份验证方法?

Basically I need to serve files from a location that requires windows authentication. Instead of having my client's deal with it directly, I would like to implement a process so that they can simply download the files as if they were on my server, after they have logged in to my system, of course. Here is what I have so far, which doesn't seem to work correctly:

// Create the request
WebRequest request = HttpWebRequest.Create(button.CommandArgument);
request.Credentials = new NetworkCredential(_username,_password);


// Get the response
WebResponse response = request.GetResponse();
StreamReader responseStream = new StreamReader( response.GetResponseStream());

// Send the response directly to output
Response.ContentEncoding = responseStream.CurrentEncoding;
Response.ContentType = request.ContentType;
Response.Write(responseStream.ReadToEnd());
Response.End();

When I try this I am able to view the file, but something is wrong with the encoding or the content type and, for example, a PDF will contain 16 blank pages (Instead of 16 pages of text).

Any idea what am I missing?

Feel free to change the title of this question if there is a better way of phrasing this question

Update:
Tried the two responses below but with no luck. I now think that the content type and encoding are OK, but maybe the authentication is failing? The content-length is a lot smaller than it actually should be... Am I using the wrong method for Windows Authentication?

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

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

发布评论

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

评论(3

王权女流氓 2024-08-01 08:58:32

取决于你如何/拥有什么。 我会做一些事情。

Response.Clear() 首先删除可能已渲染的任何内容。

然后,我将添加一个标题,并设置内容处置并将其作为实际附件发送,而不是仅仅将其写入用户。

Depending on how/what you have. I would do a few things.

Response.Clear() first of all to remove anything that might have been rendered.

I would then add a header, with content-disposition set and send it down as an actual attachment, rather than just writing it to the user.

南渊 2024-08-01 08:58:32

您似乎在最后一个代码块中发送了错误的内容类型。 您发送的是用户原始请求的类型,而不是您检索到的文件的内容类型。 将此:更改

Response.ContentType = request.ContentType;

为:

Response.ContentType = response.ContentType;

It looks like you're sending the wrong content type in your last code block. You're sending the type of the user's original request instead of the content type of the file that you've retrieved. Change this:

Response.ContentType = request.ContentType;

to:

Response.ContentType = response.ContentType;
挖个坑埋了你 2024-08-01 08:58:32

如果您的问题与网络凭据有关,您可能需要尝试其他方法。 如果您向网站的应用程序池正在使用的身份授予 HTTP 访问权限,则可以避免在请求中指定用户名/密码凭据。 这还为您带来了不需要将密码存储在某处的额外好处。

If your problem is related to network credentials, you may want to try a different approach. If you grant HTTP access to the identity that the web site's application pool is using, you can avoid having to specify the username/password credentials in the request. This also gives you the added benefit of not needing to store the password somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文