简体   繁体   English

在asp.net中下载生成的html文件的C#代码

[英]c# code for download generated html file in asp.net

I am dynamically generating html file. 我正在动态生成html文件。 It is formated with Css and Images. 它使用CSS和图像进行格式化。 I am looking for code which should able to execute on button click, and ask the location for saving file. 我正在寻找应该能够在单击按钮时执行的代码,并询问保存文件的位置。 also it should download concern resource files which has referenced in html file. 还应该下载在html文件中引用的关注资源文件。 What code i have to do ? 我必须做什么代码? Can i use WebClient for the same ? 我可以使用WebClient吗?

There is no "Save As" dialog in ASP.NET. ASP.NET中没有“另存为”对话框。 Since it is running in a browser. 由于它正在浏览器中运行。 You have no access to the user's file system, including the Save As dialog. 您无权访问用户的文件系统,包括“另存为”对话框。

But you can send the user a file, as an attachment, most browsers will display a dialog asking the user whether to save the file or open it. 但是您可以向用户发送文件,作为附件,大多数浏览器会显示一个对话框,询问用户是否保存文件或打开文件。 Maybe the user will choose to save it. 也许用户会选择保存它。

Response.ContentType = "application/octet-stream" (or content type of your file).
Response.AppendHeader("content-disposition", "attachment;filename=" & strFileName)

You could create a situation where you can use WebClient if you make your file available through a uri, see this post. 您可以创建一种情况,如果您通过uri使文件可用,则可以使用WebClient,请参阅文章。

Yes, you can use WebClient. 是的,您可以使用WebClient。 Option is to use HttpRequest and HttpResponse. 选项是使用HttpRequest和HttpResponse。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM